Rails 3,通过ajax呈现局部视图

时间:2022-10-08 14:19:42

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:

我正在使用mailboxer gem制作电子邮件服务。在我的索引视图中,我有几个链接将导致收件箱/草稿...像这样:

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

Then my show_conversation js.erb

然后我的show_conversation js.erb

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

the controller (messages_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).

并且还有一个部分视图_show_conversations.html.erb(已经过测试,通过一个简单的渲染,所以没有错误)。

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).

所以我的问题是,当我点击链接时,它实际上会重定向到一个空白的标记(当它不应该,或者至少它不是我的意图,重定向,只显示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.

我已经意识到它实际上渲染了它的控制器,然后是erb.js,局部视图,我不知道为什么控制器再一次,但这次是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

我知道它不应该重定向,我相信它不应该两次调用控制器(第二个就好像它是html而不是js),但我真的不知道它为什么会发生,也没有找到通过论坛有类似的问题

2 个解决方案

#1


0  

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

#2


0  

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' %>");

#1


0  

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

#2


0  

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' %>");