1

私は実際に質問をしているのではなく、RailsMVCパターンにうまく適合するコードを書く方法についての提案(または推奨)をしています。RailsのベテランまたはMVCに精通している人が私にフィードバックをくれることを願っています。

ActiveResourceを介してRESTfulAPIアプリと通信するWebアプリがあります。API呼び出しを使用してコンテンツをフェッチおよび更新できます。それは完璧に動作します。ただし、Webアプリにはモデルがありません。それが機能する方法は、ユーザーがアクション(インデックス、ビュー、編集など)をトリガーすると、コントローラーが直接RESTAPIを呼び出してデータをフェッチ/更新します。

私の質問は次のとおりです。この方法で行うのは良い習慣ですか、それともAPIを直接呼び出すのではなく、モデルを作成してそこにデータを入力する必要がありますか?それがMVCへの実際的な妥協であるかどうか疑問に思いました。Rails(およびMVC)の使用を開始したばかりなので、これに関するアイデア、コメント、または推奨事項を受け付けています。

4

2 に答える 2

2

It's a bit of a catch-22 question. (I did wrote a huge answer but then deleted because it will be too tedious to read)

If you mean, can you implement the MVC pattern without a model, then the answer is no. The M means Model.

If they mean, can you use the MVC without using a model, then the answer is "yes", but it is no longer MVC, you have obliterated the the M i.e. Model.

I would recommend you to read MVC pattern in detail and then try to understand what your application actually trying to do.

http://c2.com/ is a very good place if you want to understand the design patterns.

  • A model is an object representing data or even activity, e.g. a database table or even some plant-floor production-machine process.
  • A view is some form of visualization of the state of the model.
  • A controller offers facilities to change the state of the model.

Now in your case (it seems): you do have a data coming through via api so I would suggest populate the model properties and propagate it across.

Also Considering Pragmatic Compromise in MVC Dealing with things sensibly and realistically in a way that is based on practical rather than theoretical considerations. Omitting the use of Model in MVC do-not sound like a good idea, and it no longer remains MVC.

Having said that It seems from your point of view you are trying to say that Rails isn't necessarily strictly MVC hence why not use the way you want to :) but I will suggest to keep the integrity of MVC (and follow the purist approach).

http://c2.com/cgi/wiki?ModelViewController

Good read: jeff Atwoods: http://www.codinghorror.com/blog/2008/05/understanding-model-view-controller.html (Feel free to skip the asp.net part)

https://stackoverflow.com/questions/1242908/in-english-what-really-is-model-view-controller

sums it all :) source is mentioned above.

enter image description here

于 2012-08-17T22:54:15.297 に答える
0

「モデルはアプリケーションデータとビジネスルールで構成されています」(ウィキペディア)

モデルは基本的に、データベース内のローカルのテーブルです。

データを保存しておらず、データを検証していない場合は、モデルは必要ありません。

コードをクリーンアップしたい場合は、ヘルパーまたは/libにいくつかの関数を配置してください。

于 2012-08-16T23:06:06.537 に答える