Aim to catch logged in user

Asked

Viewed 624 times

0

Guys I have a simple application using ruby on Rails a 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 pick the user who is logged in at the time and assign automatically when creating a new task , because at the moment I’m having to select at the time of creating the task some of the id of users that exist registered in Windows

as a beginner I tried to read the documentation of both admin and Devise Rails and can not find anything very clear that can help me

1 answer

0


Since you didn’t post your code I’ll show you an example.

In the view you will remove the select that asks you to select user Ids. You will only associate the USER to TASK in Create from your controller.

In the example below I am associating to a task the logged in user. The logic is this, your code will be different because of 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.