Reputation: 213
I'm trying to associate my users that I created with Devise to the posts . But I am getting the error mentioned int the title whenever I try to create a post logged in as a user. Thank you very much :)
class PostsController < ApplicationController
def index
@posts = Post.all
end
def new
@post = Post.new
end
def create
@post = Post.new(params.require(:post).permit(:task))
@post.user = current_user
if @post.save
redirect_to @post, alert:"Post created successfully."
else
redirect_to new_post_path, alert: "Error creating post."
end
end
def show
@post = Post.find(params[:id])
end
end
The user model
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :posts
end
Post model
class Post < ActiveRecord::Base
validates_presence_of :task
belongs_to :user
end
Upvotes: 3
Views: 5661
Reputation: 37
Actually when you want to add an index that corresponds to an association you havo to create a migration using, in this case:
rails g migration AddUserIdToPin user:references
Rails will immediately know that you want to include an index onto the second table using the User Id unique, primary, key.
Hope this helps!.
Upvotes: 1
Reputation: 451
Look into you schema.rb and under post table can you seen the user_id column. If there is no user_id you need to add a migration column to add user_id using the following command:
rails generate migration add_user_id_to_posts user_id: integer
Using this command a migration file name 20171220171727_add_user_id_to_posts.rb will be created. For you the time stamp 20141220171727 will be different. In the migration file you will see you have the following code:
class AddUserIdToPosts < ActiveRecord::Migration
def change
add_column :posts, 'user_id', :integer
end
end
If your migration looks like the above then run:
rake db:migrate
to apply the migration. Restart the server now and see if your code works or not.
Upvotes: 1
Reputation: 89
For adding user id to pin the following worked for me
rails g migration add_user_to_pin user_id:integer
Upvotes: 3
Reputation: 1637
I just did this same stuff. Mine works this way for me.
I do have a user_id column in my posts table. I verified by doing this in rails console (rails c)
Post.column_names
Which returned all the fields, and user_id was among them.
in my Post model it belongs_to :user, and in my User model it has_many :posts.
In my PostsController though, mine differs from yours.
I have
@post = Post.new(post_params)
@post.user_id = current_user.id
Which seems to work just fine. This was also written after I implemented Devise for the user
Upvotes: 0
Reputation: 1567
In your create action, try something like:
def create
@post = Post.new(params.require(:post).permit(:task))
@post.user_id = current_user.id
if @post.save
redirect_to @post, alert:"Post created successfully."
else
redirect_to new_post_path, alert: "Error creating post."
end
end
and also make sure in your Posts database table that you have an integer field called user_id
Upvotes: 0
Reputation: 13882
You probably don't have a user_id
column on your Posts table in your db.
Upvotes: 2