我正在论坛系统之上构建一个私人消息系统(使用 SO 代码)。将新消息添加到私人消息对话时,正文为空白,但时间戳和用户是正确的。
系统工作如下。对话是由第一个帖子和收件人建立的。这有效,因为它是从 SO 问题 8205284 逐字复制的。
现在我正在尝试通过 user_conversation 控制器(这是构建对话的控制器)中的编辑/更新操作向对话添加新消息。
路线:
resources :users do
resources :conversations, :controller => "user_conversations"
end
resources :conversations, :controller => "user_conversations" do
resources :messages
end
控制器:
def edit
@conversation = UserConversation.find(params[:id])
@message = Message.new
end
def update
@conversation = UserConversation.find(params[:id])
@message = @conversation.messages.build(params[:message])
@message.user = current_user
@message.conversation_id = @conversation.conversation_id
@message.body = "without this test line the message body is blank!"
if @message.save!
redirect_to user_conversation_path(current_user, @conversation)
else
redirect_to @conversation
end
end
查看以显示对话和嵌入的新消息表单:
<%= form_for(@conversation) do |c| %>
<div class="field">
<%= c.fields_for :messages do |m| %>
<%= m.text_area :body, placeholder: "New message..." %>
<%= m.submit "New Convo Message", class: "btn btn-large btn-primary" %>
<% end %>
</div>
<%= c.submit "New Convo Message", class: "btn btn-large btn-primary" %>
<% end %>
日志输出:
Processing by UserConversationsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"InYNwRo47ec6meePrKci3z95yTgKC4K7ytO8wFHuFns=", "user_conversation"=>{"messages"=>{"body"=>"testing"}}, "commit"=>"New Convo Message", "user_id"=>"18", "id"=>"18"}