gpt4 book ai didi

ruby-on-rails - 邮箱 : how to display a specific conversation?

转载 作者:太空宇宙 更新时间:2023-11-03 16:02:22 30 4
gpt4 key购买 nike

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

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

这段代码来自对话索引 View

<%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 实例变量在我的对话 Controller 中定义

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

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

最佳答案

我假设如果有人点击对话链接,它将转到您的对话 Controller 并查找 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

在你的索引 Controller 中你需要做的:

    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看看结果

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

关于ruby-on-rails - 邮箱 : how to display a specific conversation?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21715596/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com