Render Partial UI Elements

主宰稳场 提交于 2019-12-12 01:38:36

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!