开发者

Are view models used in rails?

I'm starting to develop a small application in ruby on rails and many questions arise. I should say that I have about 1 year of experience with ASP.NET MVC and feel at home with models views and controllers. I've been using view models extensively (with the help of AutoMapper) and now wondering i开发者_StackOverflow中文版f view models are used similarly in rails camp.

From various examples (rails casts mainly) I've gathered that it is common to either combine data from multiple models right in your view (which is frowned upon in ASP.NET MVC), or to use virtual attributes on models to obtain "missing" data.

I know that business model should not be modelled after UI needs, for example there should not be a 'password confirm' property in your model, this should be a view model property.

Rails virtual properties seem to violate this principle.

How is it done in rails?

Thanks.


If I understand the notion of View Model correctly, it's a concept that's not immediately obvious in Rails, but neither is it forbidden/frowned-on or otherwise not allowed. There's no specific requirement for a perfect one-to-one mapping of models to tables so you're free to work at the level of abstraction that's appropriate.

I don't think referencing multiple models in a view is considered particularly bad (if it is, then I have some areas that need a little rework) although updating multiple models from a single view can get a bit tricky.

Anyway, in Rails I think we're talking about what seems to be mostly referred to as the "Presenter" pattern. Some references (Google "Rails Presenter Pattern" for more than you probably want or need):

  • http://blog.jayfields.com/2007/03/rails-presenter-pattern.html
  • http://gilesbowkett.blogspot.com/2007/10/my-version-of-rails-presenters.html
  • http://blog.jayfields.com/2007/10/extending-rails.html (mentions that DHH used presenters in Highrise)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜