如何解决GET 与一个控制器 POST 到另一个
我正在尝试从对话控制器呈现的表单向消息控制器发送消息,我正在通过 hidden_field_tag 传递对话 ID,但出现错误 ActiveRecord::RecordNotFound(找不到 MailBoxer::Conversation with 'id '={:value=>8}
这是我的表格:
<%= form_tag reply_to_conversation_for_chat_view_path,id: "chat_message_form",method: :post,remote: true,multipart: true do %>
<%= hidden_field_tag :recipients,value: @recipients.first.id %>
<%= hidden_field_tag :sender,value: current_user.id %>
<%= hidden_field_tag :subject,value: 'No subject' %>
<%= hidden_field_tag :conversation_id,value: @conversation.id %>
<div class="field">
<%= text_area_tag :body,nil,rows: 3,cols: 75 %>
</div>
<div class="field">
<%= label_tag 'Attachment' %><br>
<%= file_field_tag :attachment %>
</div>
<br>
<div class="submit">
<%= submit_tag 'Send' %>
</div>
<% end %>
这是消息控制器中的 set_conversation 例程:
@conversation = current_user.mailBox.conversations.find(conversation_id)
解决方法
您的问题是您使用 value: @conversation.id
作为值,即实际的键值对。 hidden_field_tag
的第二个参数就是值:
= hidden_field_tag :conversation_id,@conversation.id
https://apidock.com/rails/v6.0.0/ActionView/Helpers/FormTagHelper/hidden_field_tag
这会给你一个带有 params[:conversation_id]
而不是 "8"
的 "{value: 8}"
。
对于其余的隐藏字段,您一定有同样的问题。
,我通过将conversation_id添加到表单定义中解决了这个问题
<%= form_tag reply_to_conversation_for_chat_view_path(@conversation.id),id: "chat_message_form",method: :post,remote: true,multipart: true do %>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。