Can't send variable to partial use Cocoon (Rails 3)
I am using Rails 3 with a standard rail shape and a Cocoon gem. I want to send a variable to a partial via link_to_add_association using the "render_options" provided by gem, but I cannot get it to work. The view from partial rendering ok and coocon works fine (can add and remove items) except for the variable. Trying to print a variable in the view says it is undefined. This is my code:
_form.html.erb (this is the partial name from new.html.erb)
<%= link_to_add_association raw('Nuevo con empleado existente'), f, parte_diario_item_indirectos, :render_options => {:locals => {:foo => 'bar'}}%>
_parte_diario_item_indirecto_fields.html.erb
<%= foo %>
Doing something like this results in an "undefined 'foo" error.
I've also tried:
<%= locals[:foo] %>
with the same result. Using parentheses / curly braces around "render_options" didn't work either.
source to share
I had a very similar problem until I figured out what is render
also being called in the block fields_for
right above my calllink_to_add_association
Here's an example that Cocoon suggests, with my locals added to link_to_add_association
:
= simple_form_for @project do |f|
= f.input :name
= f.input :description
%h3 Tasks
#tasks
= f.simple_fields_for :tasks do |task|
= render 'task_fields', :f => task
.links
= link_to_add_association 'add task', f, :tasks, :render_options => { :locals => { :foo => 'bar'} }
= f.submit
I didn't go through locales to the first call render
, so the actual undefined error was from wrong render
, not fromlink_to_add_association
source to share