devise get logged in user

-1

I have a simple application using ruby on rails to gem rails admin and devise to authenticate

I have a model called task where every task belongs to a user, so I would like to get the user who is logged in at the moment and assign it automatically at the time of creating a new task, because at the moment I am having to select at the time to create any task of the id of the users that exist registered in devise

As a beginner, I tried to read the documentation of both rails admin and devise and can not find anything very clear that can help me.

    
asked by anonymous 28.03.2017 / 15:33

1 answer

0

As you have not posted your code I will show you an example.

In the view you will remove the select that asks you to select the user IDs. You will only associate the USER with the TASK on the Create of your controler.

In the example below I am associating a logged in user with a task. The logic is this, your code will be different because of the validations, redirects etc ...

def create
   #crio o objeto com os parametros que a view me enviou.
   @task = Task.new(params_task)

   #No objeto task criado estou associando o usuario logado
   @task.user = current_user
   if @task.save
       #se tudo deu certo voce redireciona
       redirect_to root_path
   else
       render :new
   end
end
    
30.03.2017 / 05:02