views:

36

answers:

1

I have the following models with associations as given below:-

class Comment < ActiveRecord::Base
   belongs_to :post
   belongs_to :user
end
class Post < ActiveRecord::Base
   belongs_to :user
   has_many :comments
end
class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable

  attr_accessible :email, :password, :password_confirmation, :remember_me
  has_many :posts
  has_many :comments
end  

But when I try to access's comment's user details I GET NO METHOD ERROR :(.
The error displayed in browser is as below:-

undefined method `email' for nil:NilClass


1: <p>
2:   <% @post.comments.each do |comment| %>
3:    <b>Comment written by:</b>   <%= comment.user.email %><br />
4:       <%= comment.body %><br />
5:   <% end %>
6: 

My schema is as below:-

create_table "comments", :force => true do |t|  
    t.integer  "post_id"  
    t.integer  "user_id"  
    t.text     "body"  
   ....  truncated 
end

create_table "posts", :force => true do |t|
    t.integer  "user_id"
    t.integer  "sell_or_buy"
    t.string   "title"
    t.text     "body"
   ....  truncated
end

create_table "users", :force => true do |t|
  t.string   "email",                               :default => "", :null => false
  t.string   "encrypted_password",   :limit => 128, :default => "", :null => false
   ....  truncated
end  

My comments create method is as follows:-

class CommentsController < ApplicationController
   def create
      @post = Post.find(params[:post_id])
      @comment = @post.comments.create(params[:comment])
      @comment.user_id = current_user.id
      redirect_to post_path(@post)
   end
end

As you can see I used devise for user model .
Any idea of what I'm doing wrong?Please help me out !!!
I'm using Rails 3.0.1

+3  A: 

I believe that you are not saving the @comment after assigning it's user_id. You're doing @comment.user_id = current_user.id, but this change is not reflected in the database.

You could do something like:

def create
  @post = Post.find(params[:post_id])
  @comment = @post.comments.new(params[:comment])
  @comment.user_id = current_user.id
  @comment.save
  redirect_to post_path(@post)
end
cristian
Nope, even after adding @comment.save I see no change in behaviour. Its still not working :(
Saran
this solution seems works. in theorie. Are you sure your comment is really save twice. one with the create and second with the save. Check on your log and check if save return true.
shingara
yup, sorry , actually it throws error on @comment.save. So save itself is not working fine :( here goes my logs:- "You have a nil object when you didn't expect it! You might have expected an instance of ActiveRecord::Base. The error occurred while evaluating nil.save "
Saran
Also this is my request params:- "{"comment"=>{"body"=>"Sample commnet text"}, "commit"=>"Create Comment", "authenticity_token"=>"A0xUERcTFKIZLPrj8tHMz7uRLOCc528X79X5HInZw2s=", "utf8"=>"✓", "post_id"=>"4"}"
Saran
For some reason, it looks like you're not actually creating a comment. If you put @comment = Comment.create!(params[:comment]) instead of @post.comments.create(params[:comment]), do you get an error message?
monocle
Hey ppl it works actually . . at one of the places I just used the plural form of comments which screwed-up the entire stuff. Sorry for wasting a few extra time. Thanks for the help. @comment.save did the trick well :)
Saran