I've read/researched other answers, but I can't get this to work. I have a js.erb file that is rendering a partial. I'm looking to pass a @node variable to it but I get an error in the console NameError: - undefined local variable or method "node" for #<#<Class:0x007fdcd9163510>:0x007fdcd813b728>
I'm not sure why it won't grab it.
_node.html.erb (node is a local variable in this partial):
<div id="tagged_users">
<%= render partial: "nodes/user_tag_list", locals: { node: node } %>
</div>
partial that I would also like rendered in js.erb
_user_tag_list.html.erb:
<ul>
<% node.tagged_users.map(&:name).each do |tag| %>
<li><%= tag %>
<%= link_to "x", node_path(node, remove_tag: tag), method: :patch %>
</li>
<% end %>
</ul>
node#update controller action:
def update
@node = Node.find(params[:id])
if params.fetch(:node, {}).fetch(:autocomplete_tag_names, false)
tag = params[:node][:taggable_email]
@node.user_tag_list.add(tag)
@node.save
respond_to do |format|
format.js { render 'user_tag_add.js.erb' }
end
elseif
# ...
else
# ...
end
and finally, the js.erb file:
user_tag_add.js.erb:
$("#tagged_users").html("<%= escape_javascript(render 'user_tag_list', locals: { node: @node }) %>");
A couple of notes. The logic is sound. If I replace the html with a string rather than the partial it works fine. Also, if I binding.pry
I can set node to @node without issue. Just can't get the @node to pass to node within the js.erb file.
EDIT
Here's the form partial, if useful:
<%= form_for node, remote: true, html: { class: "edit_node tag-users" } do |f| %>
<%= f.label :autocomplete_tag_names %>
<%= f.text_field :autocomplete_tag_names, data: { autocomplete_source: users_path }, value: nil %>
<%= f.hidden_field :taggable_email, value: nil %>
<%= f.submit %>
<% end %>
Aucun commentaire:
Enregistrer un commentaire