【问题标题】:Mailboxer : how to display a specific conversation?Mailboxer:如何显示特定的对话?
【发布时间】:2014-02-11 23:39:47
【问题描述】:

我想为每个用户显示正在进行的对话列表。所以他们只需点击它并显示他们想要的对话。我很难找到如何建立这个链接,因为邮箱中的对话对象没有 ID。

这个id好像是保存在通知对象里的,所以我尝试了这个选项。

这段代码来自对话索引视图

<%all_conv = current_user.mailbox.conversations%>
<%all_conv.each do |participant|%>
  <div class="ligne_conversation">
  <ul>
    <a href="conversations/<%=@conversation_id%>">
      <li>
        <%=image_tag participant.messages.last.sender.avatar.url(:thumb) %>
        <%=participant.messages.last.sender.name%>                  
        <%=participant.messages.last.body%>
      </li>
    </a>
  </ul></div>

@conversation_id 实例变量在我的对话控制器中定义

def index
  if current_user.mailbox.conversations.any?
  notification = Notification.find_by!(params[:id])
  @conversation_id = notification.conversation_id
  end
end

它不起作用:所有链接都指向 id = 1 的对话。

【问题讨论】:

    标签: ruby-on-rails ruby mailboxer


    【解决方案1】:

    我假设如果有人单击对话链接,它将转到您的对话控制器并查找 def show 而不是 def index。因此你需要有类似的东西:

        def show
           @conversation = Conversation.find(params[:id])
          respond_to do |format|
          format.html # show.html.erb
          format.json { render json: @conversation }
         end
        end
    

    在你的索引控制器中你需要做:

        def index
           @conversations = current_user.mailbox.conversations
    

    最后,在你看来,你会像这样迭代@conversations

        @conversations.each do |c|
         <a href="conversations/<%= c.id %>
    

    您可能需要查找 link_to 和 url_for 以使其更加优雅,但上述方法将起作用。

    这里有一个非常简单的建议,可以帮助您开始 Rails 冒险:

    创建一个新的 Rails 应用程序:

        rails new learncrud
        cd learncrud
        rails g scaffold thing name body something:int
        rake db:migrate
        rails s 
    

    然后去 127.0.0.1:3000/things 看看结果

    完成后,在您喜欢的文本编辑器中打开应用程序,然后查看脚手架如何创建页面和控制器操作。也许你已经做到了,也许没有,但它确实有助于快速掌握如何在 Rails 中完成事情。在 href 中硬编码控制器名称肯定不是最好的做事方式。

    【讨论】:

    • 感谢您的回答,它成功了。我忘了提到我已经在我的表演动作中添加了@conversation = Conversation.find(params[:id])。我误解了(也许仍然)如何找到这个特殊的 id。你是对的:这是我经过一段时间的教程后的第一个应用程序。我会听从你的建议,我有很多东西要学,不会有什么坏处。
    • railscasts.com 是大量知识的重要来源,其中许多教程都是免费提供的。 Ryan Bates 的一流视频。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-28
    • 2010-09-26
    • 1970-01-01
    • 2014-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多