开发者

Rails: Can't get fields_for to work inside a nested resource

I'm trying to add a fields_for attribute to a nested rails form. Any time I try to create a new object开发者_JAVA百科, 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜