Note that there are some explanatory texts on larger screens.

plurals
  1. PONo Method Error for "comments", can't find and correct the nil value to make the comment post
    primarykey
    data
    text
    <p>I'm relatively new to rails and am trying to pull off my first polymorphic association with comments. </p> <p>I am running rails 3.2.3</p> <p>Edit - When I try to post a comment, my log is returning this error:</p> <pre><code>Started POST "/comments" for 127.0.0.1 at 2012-05-20 13:17:38 -0700 Processing by CommentsController#create as HTML Parameters: {"utf8"=&gt;"✓", "authenticity_token"=&gt;"SOLcF71+WpfNLtpBFpz2qOZVaqcVCHL2AVZWwM2w0C4=", "comment"=&gt;{"text"=&gt;"Test this comment"}, "commit"=&gt;"Create Comment"} User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = 101 LIMIT 1 Completed 500 Internal Server Error in 126ms NoMethodError (undefined method `Comment' for nil:NilClass): app/controllers/comments_controller.rb:13:in `create' </code></pre> <p>I have tried out many different solutions offered on SO and elsewhere, including the answer from Jordan below, due, I'm sure, to my own inexperience, but have been unable to resolve the error.</p> <p>The trace calls out line 13 in the Comments Controller and I commented after that line below to mark the error:</p> <pre><code>class CommentsController &lt; ApplicationController def index @commentable = find_commentable @comments = @commentable.comments end def new @post = Post.find(params[:post_id]) end def create @commentable = find_commentable @comment = @commentable.comments.build(params[:comment]) #&lt;&lt;&lt;&lt;LINE 13 if @comment.save flash[:notice] = "Successfully created comment." redirect_to :id =&gt; nil else render :action =&gt; 'new' end end private def find_commentable params.each do |name, value| if name =~ /(.+)_id$/ return $1.classify.constantize.find(value) end end nil end end </code></pre> <p>Posts Controller:</p> <pre><code>def show @post = Post.find(params[:id]) respond_to do |format| format.html # show.html.erb format.json { render :json =&gt; @post } end end </code></pre> <p>Comment template (in post show)</p> <pre><code>&lt;ul id="comments"&gt; &lt;% if @comments %&gt; &lt;h2&gt;Comments&lt;/h2&gt; &lt;% @comments.each do |comment| %&gt; &lt;li&gt;&lt;%= comment.text %&gt;&lt;/li&gt; &lt;% end %&gt; &lt;% else %&gt; &lt;h2&gt;Comment:&lt;/h2&gt; &lt;% end %&gt; &lt;/ul&gt; &lt;%= simple_form_for [@commentable,Comment.new], :html =&gt; { :class =&gt; 'form-horizontal', :multipart =&gt; true } do |f| %&gt; &lt;fieldset&gt; &lt;%= f.input :text %&gt; Upload Photo &lt;%= f.file_field :photo %&gt; &lt;/fieldset&gt; &lt;div class="form-actions"&gt; &lt;%= f.submit nil, :class =&gt; 'btn btn-primary' %&gt; &lt;/div&gt; &lt;% end %&gt; </code></pre> <p>Post show:</p> <pre><code>&lt;p id="notice"&gt;&lt;%= notice %&gt;&lt;/p&gt; &lt;div class="row"&gt; &lt;div class="span2 offset1"&gt; &lt;%= image_tag @post.photo.url(:show) %&gt; &lt;/div&gt; &lt;div class="span5"&gt; &lt;h1&gt;&lt;%= @post.title %&gt;&lt;/h1&gt; &lt;p&gt;&lt;%= @post.index_text.html_safe %&gt;&lt;/p&gt; &lt;p&gt;&lt;%= @post.show_text.html_safe %&gt;&lt;/p&gt; &lt;%= render "comments/comment" %&gt; &lt;%= render "comments/form" %&gt; &lt;% if can? :update, @course %&gt; &lt;%= link_to 'Edit Post', edit_post_path(@post), :class =&gt; 'btn btn-mini' %&gt; &lt;%= link_to 'Delete Post', @post, confirm: 'Are you sure?', method: :delete, :class =&gt; 'btn btn-mini' %&gt; &lt;%= link_to 'New Post', new_post_path, :class =&gt; 'btn btn-mini' %&gt; &lt;% end %&gt; &lt;/div&gt; &lt;nav class="span2 offset1"&gt; &lt;ul class="well"&gt; &lt;li&gt;Category 1&lt;/li&gt; &lt;li&gt;Category 2&lt;/li&gt; &lt;/ul&gt; &lt;/nav&gt; &lt;/div&gt; &lt;div class="row offset2"&gt; &lt;%= link_to 'Back to Posts', posts_path, :class =&gt; 'btn btn-mini' %&gt; &lt;/div&gt; </code></pre> <p>Routes:</p> <pre><code> resources :posts, :has_many =&gt; :comments resources :comments </code></pre> <p>It is probably something obvious that someone with more experience can resolve. Let me know if anything comes to mind. Brian</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload