Rails 3 - Controller 'new' action with nested resource returning 'NoMethodError'
I created a message board for my application and uses the following three models to work the whole thing: Forums have many topics, and topics have many posts. Posts is a nested resource of Topic, and whenever a user chooses "Create New Topic," the 'new' action has a nested post to start the thread. Here is the relative code of that...
topics_controller.rb
class TopicsController < ApplicationController
load_and_authorize_resource
def new
@topic = Topic.new
@post = @topic.posts.build
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @topic }
end
end
[...]
topic.rb
class Topic < ActiveRecord::Base
has_many :posts
belongs_to :user, :counter_cache => TRUE
belongs_to :forum, :counter_cache => TRUE
validates :title, :length => { :maximum => 95 }, :presence => { :message => "You need to title your topic." }
accepts_nested_attributes_for :posts
end
post.rb
class Post < ActiveRecord::Base
belongs_to :user, :counter_cache => TRUE
belongs_to :forum, :touch => TRUE, :counter_cache => TRUE
belongs_to :topic, :touch => TRUE, :counter_cache => TRUE
validates :body, :presence => { :message => "You have not written any text in the body." }
end
new.html.erb
<%= form_for(@topic, :url => forum_topics_path) do |f| %>
<%= f.label :title %><%= f.text_field :title %>
<%= fields_for(@post) do |cf|%>
<%= cf.label :body %><%= cf.text_area :body, :cols=> 108, :rows => 10 %>
<% end %>
<%= f.submit %>
<% end %>
Anyway, I completed this and it worked fine.
Up until now. Just out of the blue for no reason I can spot whatsoever, whenever I select "Create a new topic" and directed to the 'new' action on Topics, I get a error of "NoMethodError in Topics#new: You have a nil object when you didn't expect it! You might have expected an instance of Array. The error occurred while evaluating nil.[]"
This makes absolutely no sense as I looked back at previous versions in git, and there have been no earth shattering changes made to this code. Perhaps some updated Rails version (3.0.7) or Gem is responsible, but I dunno about that.
Anyway, the line it's throwing a fit on is in the html.erb file, <%= fields_for(@post) do |cf|%>. Uh...it's new so it's supposed to be nil, right? Take this out, and it works. I've tried redefining @post in the Topic controller as Post.new, but that returns the same error. In addition, anything I've googled on the subject says @post = @topic.posts.build is the way to go.
So am I doing something wrong in the code above? And any idea why 开发者_开发问答it would work previously, but not now?
Try this, make sure it's f.fields_for and not just fields_for.
<%= form_for(@topic, :url => forum_topics_path) do |f| %>
<%= f.label :title %><%= f.text_field :title %>
<%= f.fields_for(@post) do |cf|%>
<%= cf.label :body %><%= cf.text_area :body, :cols=> 108, :rows => 10 %>
<% end %>
<%= f.submit %>
<% end %>
I guess there are some problems in your form, try following:
<%= form_for(@topic, :url => forum_topics_path) do |f| %>
<%= f.label :title %><%= f.text_field :title %>
<%= f.fields_for :posts do |cf|%>
<%= cf.label :body %><%= cf.text_area :body, :cols=> 108, :rows => 10 %>
<% end %>
<%= f.submit %>
<% end %>
精彩评论