Rails:无法让 fields_for 在嵌套资源中工作

发布于 2024-09-28 19:01:07 字数 2911 浏览 3 评论 0原文

我正在尝试将 fields_for 属性添加到嵌套的 Rails 表单中。每当我尝试创建一个新对象时,它都会返回

预期消息(#58819400),已收到 数组(#18903800)...

app/controllers/discussions_controller.rb:53:in `创建'

如果我尝试从基于非嵌套资源的表单访问嵌套 fields_for (又名“form_for @parent”而不是“form_for [@parent, @child]”,它工作得很好。下面的代码 - 对此的任何帮助都非常感谢控制器

  # GET /discussions/new
  # GET /discussions/new.xml
  def new
    @forum = Forum.find(params[:forum_id])
    @discussion = Discussion.new
    @discussion.messages.build

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @discussion }
    end
  end


  def create
    @forum = Forum.find_by_id(params[:forum_id])
    @discussion = @forum.discussions.new(params[:discussion])
    @discussion.user = current_user

    respond_to do |format|
      if @discussion.save
        format.html { redirect_to([@forum, @discussion], :notice => 'Discussion was successfu#ly created.') }
        format.xml  { render :xml => [@forum, @discussion], :status => :created, :location => @discussion }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @discussion.errors, :status => :unprocessable_entity }
      end
    end
  end

模型:

class Forum < ActiveRecord::Base
  belongs_to :user
  has_many :discussions, :dependent => :destroy
  validates :title, :presence => true

  accepts_nested_attributes_for :discussions, :allow_destroy => true
end

class Discussion < ActiveRecord::Base
  belongs_to :user
  belongs_to :forum
  has_many :messages, :dependent => :destroy
  validates :title, :presence => true
end

class Message < ActiveRecord::Base
  belongs_to :user
  validates :user, :presence => true
  validates :content, :presence => true
end

视图:

<%= form_for [@forum, @discussion] do |f| %>
  <% if @discussion.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@discussion.errors.count, "error") %> prohibited this discussion from being saved:</h2>

      <ul>
      <% @discussion.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </div>
  <%= f.fields_for :messages do |builder| %>
    <%= builder.label :content, "Message" %>
    <%= builder.text_area :content, :rows => 10 %>

  <% end %>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

最后,路由:

  resources :forums do
    resources :discussions do
      resources :messages
    end
  end

非常感谢任何帮助 - 我完全被难住了。

I'm trying to add a fields_for attribute to a nested rails form. Any time I try to create a new object, it returns

Message(#58819400) expected, got
Array(#18903800) ...

app/controllers/discussions_controller.rb:53:in
`create'

If I try to access nested fields_for from forms based on non-nested resources (aka "form_for @parent" instead of "form_for [@parent, @child]" it works fine. Code below - any help with this really appreciated.

Controller:

  # GET /discussions/new
  # GET /discussions/new.xml
  def new
    @forum = Forum.find(params[:forum_id])
    @discussion = Discussion.new
    @discussion.messages.build

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @discussion }
    end
  end


  def create
    @forum = Forum.find_by_id(params[:forum_id])
    @discussion = @forum.discussions.new(params[:discussion])
    @discussion.user = current_user

    respond_to do |format|
      if @discussion.save
        format.html { redirect_to([@forum, @discussion], :notice => 'Discussion was successfu#ly created.') }
        format.xml  { render :xml => [@forum, @discussion], :status => :created, :location => @discussion }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @discussion.errors, :status => :unprocessable_entity }
      end
    end
  end

Models:

class Forum < ActiveRecord::Base
  belongs_to :user
  has_many :discussions, :dependent => :destroy
  validates :title, :presence => true

  accepts_nested_attributes_for :discussions, :allow_destroy => true
end

class Discussion < ActiveRecord::Base
  belongs_to :user
  belongs_to :forum
  has_many :messages, :dependent => :destroy
  validates :title, :presence => true
end

class Message < ActiveRecord::Base
  belongs_to :user
  validates :user, :presence => true
  validates :content, :presence => true
end

The view:

<%= form_for [@forum, @discussion] do |f| %>
  <% if @discussion.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@discussion.errors.count, "error") %> prohibited this discussion from being saved:</h2>

      <ul>
      <% @discussion.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </div>
  <%= f.fields_for :messages do |builder| %>
    <%= builder.label :content, "Message" %>
    <%= builder.text_area :content, :rows => 10 %>

  <% end %>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

And finally, the routing:

  resources :forums do
    resources :discussions do
      resources :messages
    end
  end

Any help with this really appreciated - I'm completely stumped.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抚笙 2024-10-05 19:01:07

啊 - 真的很抱歉大家......我刚刚意识到我忘记了讨论模型中的 Accepts_nested_attributes_for ,&amp;因此,论坛可以进行讨论,但讨论无法深入讨论消息。

无论如何,谢谢。

Arghhh - really sorry folks...I just realised I'd forgot the accepts_nested_attributes_for in the discussions model, & consequently forums could access discussions, but discussions couldn't get down to messages.

Thanks anyhow.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文