<% if @discussion_category %>
$("#<%= dom_id(@discussion_category) %>_notice").html('');
$("#<%= dom_id(@discussion_category) %>").html("<%=escape_javascript(render(partial: 'display_change', locals: {discussion_category: @discussion_category}))%>");
$('#discussion_category_links').show();
<% end %>
I did the following, but I'm doing something wrong:
-if @discussion_category
javascript:
$(escape_javascript(#{dom_id(@discussion_category)} + '_notice').html('');
$(escape_javascript(#{dom_id(@discussion_category)}).html(#{escape_javascript(render(partial: 'display_change', locals: {discussion_category: @discussion_category}))});
$('#discussion_category_links').show();
What am I doing wrong here? Thank you for any help.
Best How To :
The escape_javascript
function is on the rails side. You don't want it in the output so it'd be within the #{}
.
And you threw out most the quotes. Still need those.
Hopefully there's no typos but a direct translation should look more like this:
-if @discussion_category
javascript:
$("#{escape_javascript(dom_id(@discussion_category))}_notice").html('');
$("#{escape_javascript(dom_id(@discussion_category))}")
.html("#{escape_javascript(render(partial: 'display_change', locals: {discussion_category: @discussion_category}))}");
$('#discussion_category_links').show();
When building messy RJS stuff like this it can be much cleaner looking to use the j
alias for escape_javascript
, which would help in narrowing down syntax issues as well:
javascript:
$("#{j dom_id(@discussion_category)}_notice").html('');
$("#{j dom_id(@discussion_category)}")
.html("#{j render(partial: 'display_change', locals: {discussion_category: @discussion_category})}");
Finally, there's probably no reason to escape the output of dom_id
, meaning you could just:
javascript:
$("#{dom_id @discussion_category}_notice").html('');
$("#{dom_id @discussion_category}")
.html("#{j render(partial: 'display_change', locals: {discussion_category: @discussion_category})}");
... which is beginning to look a lot more readable.