Forum: Ruby on Rails undefined method user_id

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cb4083ec69587eaa094351e12771957a?d=identicon&s=25 Ken Lim (imkenshin23)
on 2009-02-21 18:46
Basically, Im trying to add comments to photos that other users have
uploaded. And Ive encountered an error:

NoMethodError in CommentsController#create
undefined method `user_id=' for #<Comment:0x4768cd0>

C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1857:in
`method_missing'
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1672:in
`send'
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1672:in
`attributes='
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1671:in
`each'
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1671:in
`attributes='
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1505:in
`initialize_without_callbacks'
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/callbacks.rb:225:in
`initialize'
#{RAILS_ROOT}/app/controllers/comments_controller.rb:8:in `new'
#{RAILS_ROOT}/app/controllers/comments_controller.rb:8:in `create'

This is my comments controller:

class CommentsController < ApplicationController
  before_filter :login_required

  def create
    @photo = Photo.find_by_user_id_and_id(params[:user_id],
                                                            params[:photo_id])

    @comment = Comment.new(:user_id => @logged_in_user.id,
                                            :body =>
params[:comment][:body])
    if @photo.comments << @comment
      flash[:notice] = 'Comment was successfully created.'
      redirect_to photos_path(:user_id => @photo.user,
                                        :photo_id => @photo)
    else
      render :controller => 'photos', :action => 'show',
                  :user_id => @photo.user, :photo_id => @photo
    end
  end

  def destroy
    @photo = Photo.find_by_user_id_and_id(@logged_in_user.id,
                                                          params[:photo_id],
                                                          :include =>
:user)
    @comment = @photo.comments.find(params[:id])
    @comment.destroy

    redirect_to entry_path(:user_id => @photo.user.id,
                                    :id => @photo.id)
  end
end

It said that the user_id is undefined when creating a new comment. Any
kind soul can tell me how to solve?
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-02-22 00:42
(Received via mailing list)
On Feb 21, 5:46 pm, Ken Lim <rails-mailing-l...@andreas-s.net> wrote:
>
> It said that the user_id is undefined when creating a new comment. Any
> kind soul can tell me how to solve?

Do your comments have a user_id column ?

Fred
This topic is locked and can not be replied to.