A newbie question:
I have a partial that I’m loading on a website on different pages.
That partial needs some data, so in the controller of the parent view I do the query:
@properties = Property.find(:all)
I pass the query results to the partial in the view using :locals
Now, I would like to render the same partial from another view. This view has a different controller. Do I have to repeat the query in that controller also? Or can I have a separate controller for partials I use one more places in the website. In this last case, how do I indicate which controller to use in the view when I put the render command?
Or should I use somethink different than partials for this kind of use?
Kind regards,
Michael
You don’t need to copy the code to set up the partial’s data, but you would need to hoist it into a controller that your two controllers inherit from. Normally, this would be your
ApplicationController. Define a method in there that loads the data you need. For example:Now to actually call this method you have two options:
before_filterto yourApplicationControllerlikebefore_filter :load_partial_databefore_filterto just the controllers that actually need to load the data.I hope this helps.