0

When the user is clicking on a link with the class "edit_resource", the content of a div should be replaced by a partial. Here is my code:

$(".edit_resource").click(function(){
  var id = $(this).attr("id")
  $('#id' + id + '_show').html("<%= escape_javascript(render :partial => 'form', :locals => {:@resource => resource})%>")
})

The code is working as expected, except that that escape_javascript doesn't work. The new content of the div is the text

<%= escape_javascript(render :partial => 'form', :locals => {:@resource => resource})%>

, and this is also what is shown on the page.

No code is executed, and my partial isn't rendered. I have tried to use <%== instead of <%= without luck.

I have also tried

<%= raw escape_javascript(render :partial => 'form', :locals => {:@resource => resource})%>

I have even tried to replace the partial part of the code with just simple rails code. That didn't help either.

What can I do?

I use Rails 3.0.10, and my javascript_include_tag is like this:

<%= javascript_include_tag 'jquery-1.6.2.min', 'jquery-ui-1.8.16.custom.min', 'application', 'jquery.rails.js'%>

3 Answers 3

3

Add .html_safe after the closing bracket of escape_javascript

Sign up to request clarification or add additional context in comments.

1 Comment

Didn't help... Thank's for your reply.
1

I have put my js in a js.erb-file, and finally managed to use my variables in the right way.

Here's my code:

$('#id' + '<%= @id %>' + '_show').html('<%= escape_javascript(raw render :partial => 'form', :locals => {:@resource => Resource.find_by_id(@id)}).html_safe %>')

And in the controller: format.js { @id = params[:id]}

In the view: <%= link_to t('edit'), resources_path(:id => resource.id), :remote => true %>

Comments

0

I don't think you can have :@resource @ followed by an symbol :

<%= escape_javascript(render :partial => 'form', :locals => {:@resource => resource})%>

Just take that @ out

<%= escape_javascript(render :partial => 'form', :locals => {:resource => resource})%>

and use the variable by calling resource in your partial

1 Comment

Thanks for your reply! Using :@resource works very well in a non-ajax variant I have used for testing. I have nevertheless tried to change this according to your advise, but it didn't help. If :@resource was the problem, I think I would have got an error message. The only thing I get is the whole escape-thing as a string on my page

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.