简体   繁体   中英

Render partial with locals inside a form_for tag

I have this form (simplified)

<%= form_for @client do |client| %>
 <%= render "some_partial", foo: 10, bar: 20 %>
<% end %>

I also try with: <%= render :partial => "some_partial", :locals => {foo: 10, bar: 20} %>

And in some_partial :

<%= foo %>
<%= bar %>

But, the variables are not available on partial:

undefined local variable or method `foo' for #<#<Class:0x007fb35027bbd0>:0x007fb350536e60>

If I put render out side of form_tag works fine

Thanks in advance

UPDATE1 : At the end of form, I have this:

<%= link_to_add_association 'Add', client, :client_addresses, class: "ui mini green button add_client_address" %>

If I remove this part, works fine...

您必须像这样将参数提到为本地变量。

<%= render :partial => "some_partial", :locals => {foo: 10, bar: 20} %>

Is the "some_partial" the same :client_addresses you are trying to load with the link_to_add_association ?

I believe that the error message you are experiencing is coming from within the link_to_add_association

Seems you are using the Cocoon gem . In order to pass locals into the partial that Cocoon uses ( :client_addresses in your case) you need to pass the parameter render_options: { locals: {foo: bar} for the link_to_add_association

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM