Nested link_to_function/insert_html does not work
-
20-09-2019 - |
Question
Here is a simple example of the problem.
In short, if you have a index.rhtml with:
<%= link_to_function "A link to insert a partial with nested insert_html" do |page|
page.insert_html :top, :with_a_nested_insert_html, :partial => 'example_partial_with_nested_insert_html'
end %>
And a _example_partial_with_nested_insert_html.rhtml
<%= link_to_function "A nested link to insert_html" do |page|
page.insert_html :top, :with_a_nested_insert_html, :partial => 'you_wont_see_this'
end %>
It breaks the "A link to insert a partial with nested insert_html". I am thinking something needs to be done to protect the javascript in the partial.
Any thoughts?
Solution
Here's how I do it.
<%= link_to_function( "insert it", :id => 'foo') do |page|
partial = escape_javascript(render :partial => "my_partial", :object => Object.new)
page << "$('#my_div').append(\"#{partial}\")"
end %>
OTHER TIPS
Try using escape_javascript before rendering the partials - see this other question.
I didn't try but I strongly think the syntax should be more something like :
<% link_to_function "A link to insert a partial with nested insert_html" do |page|
<%= page.insert_html :top, :with_a_nested_insert_html, :partial => 'example_partial_with_nested_insert_html' %>
<% end %>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow