简体   繁体   中英

Rails 3, rendering partial view through ajax

I'm making an e-mail service with mailboxer gem. In my index view I have several links that will lead to inbox/draft... like this:

<%= link_to "Inbox", show_conversations_path, :remote => true %>
...
<div id="content">
</div>

Then my show_conversation js.erb

$("#content").html("<%=escape_javascript(render :partial=>"show_conversations")%>");

the controller (messages_controller)

def show_conversations
  @inbox = current_user.mailbox.inbox
  respond_to do |format|       
    format.js  
  end
end

And there's also a partial view _show_conversations.html.erb (already tested, through a simple render so no errors there).

So my problem is that, when I click on the link, it actually redirects to a blank plage (when it shouldnt, or at least it's not my intention, redirect at all, just show the content on the div).

I've realized that it actually renders its controller, then the erb.js, the partial view, and I Don't know why the controller again but this time as HTML.

A piece of the log:

Started GET "/en/messages/show_conversations"4
Processing by MessagesController#show_conversations as JS
...
  Rendered messages/_show_conversations.html.erb (7962.4ms)
  Rendered messages/show_conversations.js.erb (23239.0ms)
Completed 200 OK in 23330ms (Views: 23237.9ms | ActiveRecord: 5.0ms)

Started GET "/en/messages/show_conversations
Processing by MessagesController#show_conversations as HTML
Completed 406 Not Acceptable in 8ms (ActiveRecord: 0.5ms)

I know it shouldn't redirect, and I believe it shouldn't call two times to the controller (and the second one as if it was html instead of js), but I don't really know why it's happening nor have I found a similar problem through the forum

Check with below code, if it works

def show_conversations
 @inbox = current_user.mailbox.inbox
 respond_to do |format|       
   format.js { render :layout => false }
 end
end

Try modifying this line:

$("#content").html("<%=escape_javascript(render :partial=>"show_conversations")%>");

to:

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

or make it pretty:

$("#content").html("<%= j render :partial=>'show_conversations' %>");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM