Get session data in rails

I am new to rails coming from php and I am learning rails 3 with mongodb.

I got to the point that I have html message text for mongo and I can create and log in as a user.

In user login, I create a session with a great authenticator created

session[:user_id] = user.id

Now I am trying to get this user_id from the session when I create a new message so that the message is connected to the user.

in my post_controller, I have a create function as follows.

def create
    @post = Post.new(params[:post])
    respond_to do |format|
      if @post.save
        format.html { redirect_to(@post, :notice => 'Post was successfully created.') }
        format.xml  { render :xml => @post, :status => :created, :location => @post }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @post.errors, :status => :unprocessable_entity }
      end
    end
end

however, I believe that I need to pass user_id here, or maybe it is better to put it in my model.

But an attempt of any kind

@user_id = session[:user_id]

did not return anything meaningful. Am I going about it wrong?

, , , - , user_id.

+3
2

, db? post :

: user

. user_id post db, ( , , .)

+1

: Nifty Authentication

Nifty Authentication current_user ( Rails ), , , . , :

post.user = current_user

, , .

+1

Source: https://habr.com/ru/post/1752712/


All Articles