Pregunta

I am asked to draw the sequence diagram to represent the MVC pattern in Ruby on rails. Since sequence diagrams is an "object diagram", the models, controllers, views should be objects. In Ruby on rails, there are objects of models, controllers, but are there any "view objects " in rails?

¿Fue útil?

Solución

The view objects in Rails are there, but they're hidden behind the template infrastructure.

When the action method in the controller object wants to render a page - either by explicitly calling render or implicitly by returning - the Rails infrastructure calls into a subclass of ActionView::TemplateHandlers, passing in your .erb template and any instance variables set in the controller. The template handlers parse out the HTML bits, find the ruby bits embedded in <% %> tags, and build an object out of it. It then calls a method in the object to do the rendering. You can see evidence of this in the stack traces that get generated when there's an error in the code in your view. I generated the below error by putting <% 0 / 0 %> in a view:

divided by 0 (ActionView::Template::Error)
  ./app/views/home/index.html.erb:9:in `/'
  ./app/views/home/index.html.erb:9:in `block in _app_views_home_index_html_erb___2383790952439350653_70222507697420'
  /Users/dpassage/.rvm/gems/ruby-1.9.3-p327/gems/actionpack-3.2.11/lib/action_view/helpers/capture_helper.rb:40:in `block in capture'
  /Users/dpassage/.rvm/gems/ruby-1.9.3-p327/gems/actionpack-3.2.11/lib/action_view/helpers/capture_helper.rb:187:in `with_output_buffer'
...

The method _app_views_home_index_html_erb___2383790952439350653_70222507697420 was generated at runtime by ActionView.

The upshot is that you can think of those .erb templates as being the view objects in the MVC. If you have helper methods defined to help with the rendering process, they're part of the view layer as well.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top