Ruby on rails Rails嵌套表单,未传递属性

Ruby on rails Rails嵌套表单,未传递属性,ruby-on-rails,ruby-on-rails-3.2,nested-forms,nested-attributes,Ruby On Rails,Ruby On Rails 3.2,Nested Forms,Nested Attributes,因此,我有一个对话模型,其中包含许多消息。我试图在创建对话时创建新消息。这是我的对话控制器: class ConversationsController < ApplicationController before_filter :authenticate_user! def new recipient = User.find(params[:user]) @conversation = Conversation.new(from: current_user, to

因此,我有一个
对话
模型,其中
包含许多
消息
。我试图在创建对话时创建新消息。这是我的
对话控制器

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
问题是:当我提交表单时,会话的消息会被保存,但是我在
构建中指定为参数的
to
from
字段不会被保存(它们是零)。但是,此表单中填写的
主题
内容
字段可以正常保存

我做了一点挖掘。。。如果我在
new
操作中或在
new.html.erb
中对
@conversation.messages
执行
操作,则该消息似乎包含
。只有当消息到达“创建”操作时,这些字段才会消失。

更新:

class ConversationsController < ApplicationController
  before_filter :authenticate_user!
  def new
    recipient = User.find(params[:user])
    @conversation = Conversation.new(to: recipient)
    @conversation.messages.build
  end

  def create
    @conversation = current_user.conversations.build(params[:conversation])

    # Set all the attributes for conversation and messages which
    # should not be left up to the user.
    @conversation.to = current_user
    @conversation.messages.each do |message|
      message.to = @conversation.to
      message.from = @conversation.from
    end

    redirect_to @conversation
  end
end

<%= form_for @conversation do |f| %>
  <%= f.hidden_field :recipient %>
  <%= 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 %>
类会话控制器

您可能仍想验证会话模型中的收件人。

我可以考虑从会话中删除<代码> >代码> >代码> >,我认为这会更有意义…在给定的会话中,我可以发送一个消息给你,你可以发送一个消息给我,反转<代码>到< /代码>和<>代码>从每次…2件事:除了隐藏的字段之外,没有其他方法可以做到这一点吗?另外,如何验证收件人?您需要某种方法来跟踪请求中的收件人。您可以将其作为URL参数(可能作为嵌套资源),而不是使用隐藏字段。验证由您决定,它将取决于您的应用程序。用户应该能够提交任何收件人ID,还是收件人应该是对话的两个成员之一?一些ActiveRecord验证应该有助于检查您需要的任何条件。
class ConversationsController < ApplicationController
  before_filter :authenticate_user!
  def new
    recipient = User.find(params[:user])
    @conversation = Conversation.new(to: recipient)
    @conversation.messages.build
  end

  def create
    @conversation = current_user.conversations.build(params[:conversation])

    # Set all the attributes for conversation and messages which
    # should not be left up to the user.
    @conversation.to = current_user
    @conversation.messages.each do |message|
      message.to = @conversation.to
      message.from = @conversation.from
    end

    redirect_to @conversation
  end
end

<%= form_for @conversation do |f| %>
  <%= f.hidden_field :recipient %>
  <%= 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 %>