How to pass string into Rails partial?

て烟熏妆下的殇ゞ 提交于 2019-12-07 01:48:19

问题


I have a view that is like this:

render  :partial  => 'shared/_address', :locals   => {:address => order.bill_address}

With a partial that looks like this:

  <b><%= address.name %></b><br/>
  <%= raw address.address_lines('<br/>') %><br/>
  <%= address.city_state_zip %><br/>

There are multiple instances of the partial rendered on a page. Instead of having it display address.name, how can I modify my render :partial line so that I am passing on a custom string e.g. "Future Shipping Address" instead of having to use address.name?

So the code would look like below:

  <b>STRING GOES HERE</b><br/>
  <%= raw address.address_lines('<br/>') %><br/>
  <%= address.city_state_zip %><br/>

回答1:


Try:

render :partial => 'shared/address', :locals => {:my_string=>"my string", :address => order.bill_address}

in partial

<b><%= my_string %></b><br/>
<%= raw address.address_lines('<br/>') %><br/>
<%= address.city_state_zip %><br/>



回答2:


view:

render 'shared/_address', address: order.bill_address, custom_string: 'foobar'

partial:

<b><%= custom_string %></b>
<%= raw address.address_lines('<br/>') ....


来源:https://stackoverflow.com/questions/15081526/how-to-pass-string-into-rails-partial

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