Accessing and updating Devise current user in a different controller

So I'm creating a tv trivia game app, and when a user completes their quiz, I need to update certain aspects of the user (level/xp, also saving some of their answers to the database).

However, I'm using devise for authentication, and I'm not sure how to access and update the current user when I'm in the quiz controller or quiz view. I'm assuming I need to add a helper method which gives the controller access to the current user, but I'm not sure how to do it exactly.

Thanks in advance for your help.

Joseph,

Whats the association between a quiz and a user. If they are related , then you can write a callback in your quiz model

after_create :increase_user_xp

or

after_update :increase_user_xp

def increase_user_xp

self.user.update(:xp => 50)

end

You should just be able to do that by making the controller a subclass of ApplicationController

explained here:

Well it already inherits from ApplicationController by default, and I'm unable to send an update request form to the resource from that view

Jason Fb wrote in post #1159259:

You should just be able to do that by making the controller a subclass of ApplicationController

explained here:

Well it already inherits from ApplicationController by default, and I'm unable to send an update request form to the resource from that view

It is no good saying you are unable to do something, show us the error and the section of code that causes it. Not large chunks of code, a few lines should be enough. Also the output in development.log relating to the request, if it looks useful

Colin

Yes, please specify exactly what you mean when you say "to the resource" or "from that view"

Normally you would do think about this in terms of quiz controller (but really you should pass this off to a domain layer).

Your question (the way I interpreted it) was where/how can you access the scope of current_user. The answer is explained in that stackoverflow post.

-Jason

Okay I'll try and show you what errors I'm getting later.

For now let me try to explain the situation a bit more clearly (apologies, I'm still learning, this is my first app and I'm mainly doing it to learn rails, quite possibly I'm way off on what I'm doing).

I created a quiz scaffold and used devise to set up users.

When a user logs in, they have a dashboard with a link to 'quiz'. This goes to the index action of the quiz controller, which displays all of the quizzes. I want to build the actual functionality into the quiz#show action, which gets an individual quiz form.

However, the part I'm struggling to understand is this: They 'answers' to the quiz have to belong to the user, surely? I can't have an 'answer' attribute on the quiz, because then each user would just be updating this attribute belonging to quiz each time they submit something (by the way, there is only 1 question on each quiz page).

So, I've set up the 'answer' attribute as part of the user. The idea is that the user goes to the quiz/:id page, inputs their answer, this is then sent using a put update request to edit the users 'answer' attribute and manipulate it/check it for correctness.

So my show.html.erb for quiz is something like:

<h1><%= @quiz.title %></h1>

<p>   <%= @quiz.question %> </p>

<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>   <div><%= f.label :answer %>   <%= f.text_field :answer %> </div>     <div><%= f.submit "Submit" %></div> <% end %>

Does this make sense, or is this there a far easier way of doing it?

I appreciate the help! Thanks.

Joe

Okay I'll try and show you what errors I'm getting later.

For now let me try to explain the situation a bit more clearly (apologies, I'm still learning, this is my first app and I'm mainly doing it to learn rails, quite possibly I'm way off on what I'm doing).

I created a quiz scaffold and used devise to set up users.

When a user logs in, they have a dashboard with a link to 'quiz'. This goes to the index action of the quiz controller, which displays all of the quizzes. I want to build the actual functionality into the quiz#show action, which gets an individual quiz form.

However, the part I'm struggling to understand is this: They 'answers' to the quiz have to belong to the user, surely? I can't have an 'answer' attribute on the quiz, because then each user would just be updating this attribute belonging to quiz each time they submit something (by the way, there is only 1 question on each quiz page).

So, I've set up the 'answer' attribute as part of the user. The idea is that the user goes to the quiz/:id page, inputs their answer, this is then sent using a put update request to edit the users 'answer' attribute and manipulate it/check it for correctness.

You probably want the answer to belong_to both the user and the quiz.

So my show.html.erb for quiz is something like:

<h1><%= @quiz.title %></h1>

<p> <%= @quiz.question %> </p>

<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %> <div><%= f.label :answer %> <%= f.text_field :answer %> </div>    <div><%= f.submit "Submit" %></div> <% end %>

"resource" is probably something you copied from an example (or the docs). You're supposed to put the actual object there, like @answer (assuming you have created the @answer object as an instance variable in your controller)

the url: is going to be a path correlated to the answer routes, which you will define in your routes file. You can use rake routes to figure out what the named routes are.

Start by working right through a good tutorial such as railstutorial.org (which is free to use online). That will show you the basics of Rails.

Colin

You probably want the answer to belong_to both the user and the quiz.

"resource" is probably something you copied from an example (or the docs). You're supposed to put the actual object there, like @answer (assuming you have created the @answer object as an instance variable in your controller)

the url: is going to be a path correlated to the answer routes, which you will define in your routes file. You can use rake routes to figure out what the named routes are.

resource is the way that devise refers to user. I copied it from the devise registrations edit view, which updates user attributes.

If I create a separate answer object, can I still access it through the route quiz/:id? Would this be more difficult than just linking user and quiz through HABTM?

Thanks :slight_smile:

Should answers be set up as a join table between user and quiz?

Joseph Wilks wrote in post #1159311:

Should answers be set up as a join table between user and quiz?

I've been thinking about this and I think a join table between users and quiz which stores the answer to the question, and also a boolean :passed attribute would be the correct way to handle this.