Reputation: 1
I am using devise for authentication in my rails app and was using the 'current_user' method in one of my controllers. Since then, I have set up a 'has_and_belongs_to_many' association between my User and Activity tables as shown below.
My issue is that I get the following error when the new/create methods get called in my Activities controller:
'undefined method `user_id' for #<Activity:0x007fe89c4e8400>'
Anyone have a clue what this is being caused by? Thanks!
activities_controller.rb
def create
@activity = Activity.new(params[:activity])
respond_to do |format|
if @activity.save
format.html { redirect_to @activity, notice: 'Activity was successfully created.' }
format.json { render json: @activity, status: :created, location: @activity }
else
format.html { render action: "new" }
format.json { render json: @activity.errors, status: :unprocessable_entity }
end
end
end
user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :name
# attr_accessible :title, :body
#many to many association
has_and_belongs_to_many :activities
end
activity.rb
class Activity < ActiveRecord::Base
attr_accessible :description, :image
validates :description, presence: true
validates :user_id, presence: true
validates_attachment :image, presence: true,
content_type: { content_type: ['image/jpeg', 'image/jpg', 'image/gif', 'image/png']},
size: { less_than: 5.megabytes }
has_and_belongs_to_many :user
has_attached_file :image, styles: { small: "240x180"}
end
schema.rb
ActiveRecord::Schema.define(:version => 20130630004839) do
create_table "activities", :force => true do |t|
t.string "description"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "image_file_name"
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"
end
create_table "activities_users", :id => false, :force => true do |t|
t.integer "activity_id"
t.integer "user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "users", :force => true do |t|
t.string "email", :default => "", :null => false
t.string "encrypted_password", :default => "", :null => false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", :default => 0
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "name"
end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true
end
Upvotes: 0
Views: 896
Reputation: 1694
You have to do like this :
@activity = current_user.activities.build(params[:activity])
Upvotes: 1