所以我有一个Conversation
模型,它has_many
messages
。创建对话时,我正在尝试创建新消息。这是我的ConversationsController
:
class ConversationsController < ApplicationController
before_filter :authenticate_user!
def new
recipient = User.find(params[:user])
@conversation = Conversation.new(from: current_user, to: recipient)
@conversation.messages.build(from: current_user, to: recipient)
end
def create
@conversation = Conversation.create(params[:conversation])
redirect_to @conversation
end
end
这是我的表格(conversations/new.html.erb
):
<%= form_for @conversation do |f| %>
<%= f.fields_for :messages do |g| %>
<%= g.label :subject %>
<%= g.text_field :subject %>
<%= g.label :content %>
<%= g.text_field :content %>
<% end %>
<%= f.submit "Send" %>
<% end %>
问题:当我提交表单时,对话的消息被保存,但我指定为参数的to
和字段没有保存(它们为零)。但是,在此表单中填写的和字段保存得很好。from
build
subject
content
我已经进行了一些挖掘......如果我在动作中或在 中执行puts
on ,则消息似乎具有and 。只有当消息到达操作时,这些字段才会消失。@conversation.messages
new
new.html.erb
to
from
create