问题
In rails, I often have the need to declare a partial that is used across many models. For example, there is a partial where I would like a form wrapped around a button, such that I can just pass in a controller, an action, and an object and that object is sent to that controller's action. Now this is not specific to any of my models. It doesn't seem fitting to declare this partial's file (_processor_button.html.erb) in a model's folder.
What do you suggest is the best place to put this file, and how to call it from there, and do you suggest a totally different way of approaching the example mentioned above?
回答1:
I store commonly used partials in views/shared/
and call them this way:
render :partial => "/shared/partial_name"
回答2:
I allways put these kind of partials within: app/views/shared/
Assume a partial like;
... shared/_my_form.html.erb ...
<%= form_for(my_obj) do %>
...
...
Then in your views to render this partials with different "my_obj"
... posts/form.html.erb ...
<%= render :partial => "/shared/my_form", :locals => { :my_obj => @post } %>
or like:
... users/form.html.erb ...
<%= render :partial => "/shared/my_form", :locals => { :my_obj => @user} %>
来源:https://stackoverflow.com/questions/7343808/render-partial-ui-elements