开发者

Rails 3: Ajax and the <%= escape_javaScript(item) %>

*(note that everything works perfectly outside of the Ajax. the submittable class works to submit the form onchange)

So here I've set up a basic sample, for some reason I can't get the <%= escape_javaScript(item) %> to show on the form submit. To better explain, I'll just show my relative code.

index.html.erb

<% @contacts.each do |contact| %>
    <%= form_for contact,:remote => true do |f| %>
        <div class="editField">
            <%= f.collection_select :agent_id, get_user_list("agent"), :id, :full_name, {:include_blank => 'None assigned'},:class => "submittable" %>
        </div>
    <% end %>
    <div id="agent2">
        <%= contact.agent_id %>
    </div>
<% end %>

contact_controller.rb

def update
    @contact = Contact.find(params[:id])


    respond_to do |format|
      if @contact.update_attributes(params[:contact])
        format.js { render }
      end
    end
end

update.js.erb

$('#agent2').html("<%= escape_javaScript(contact.agent_id) %>");

With that set up, I get no response or change to the (div id agent2) but if I h开发者_StackOverflowave update.js.erb using this code

$('#agent2').html("something");

The text "something" replaces the current contact.agent_id

I'm beyond confused as to why its not working correctly. I'm using Rails 3.0.3 with ruby 1.8.7 (2010-08-16 patchlevel 302) [i686-darwin10], and use Jquery.


Is there a particular reason you are calling the escape_javascript helper? It looks like you are just wanting the agent_id value to be inside the #agent2 div. Is that the case? Have you tried just $('#agent2').html("<%= contact.agent_id %>");

Typically, when I am using the escape_javascript helper method I will pass in a partial so the call will look something like:

$('#agent2').html("<%= escape_javascript( render(:partial => 'partial_name') ) %>");

Hope that helps!


escape_javascript is case sensitive and all lowercase. Can you try:

$('#agent2').html("<%= escape_javascript('something') %>");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜