ruby on rails - Current_user doesn't return a value -


my current_user function does'nt work in post controller. when try show /posts/1, 1st record maded in dbconsole recive error:

couldn't find post id=1 [where "posts"."user_id" = ?] 

see sources: post model:

class post < activerecord::base     belongs_to :user end 

post model:

  class user < activerecord::base   has_many :posts   attr_accessor :password   before_save :encrypt_password   ..... 

application controller:

  class applicationcontroller < actioncontroller::base   protect_from_forgery with: :exception   helper_method :current_user       private       def current_user     @current_user ||= user.find(session[:user_id]) if session[:user_id]       end   end 

the problem appear in post controller:

class postscontroller < applicationcontroller ...     def show     @post = current_user.posts.find(params[:id])     end ... end 

can onyone me?

def show  @post = current_user.posts.find(params[:id]) end 

the above find posts belonging current_user specified posts.id. in case posts/1, current_user.posts.find(params[:id]) fire sql query below

select "posts".* "posts" "posts"."user_id" = ? , "posts"."id" = ? limit 1  [["user_id", 1], ["id", 1]] 

make sure have post record id=1 belongs user_id=1.

you receiving error, because in post table, post entry id=1 not belong user_id=1.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -