だから私は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とフィールドは保存されません (それらは nil です)。ただし、このフォームに入力されたおよびフィールドは問題なく保存されます。frombuildsubjectcontent
少し掘り下げてみました...アクションでputson@conversation.messagesを実行するとnew、または でnew.html.erb、メッセージに と が含まれているようtoですfrom。createこれらのフィールドが消えるのは、メッセージがアクションに到達したときだけです。