Reputation: 325
I'd like to perform some calculations based on integer values in the database for the "User" class. (using devise.) This is working correctly on users/show.html.erb, but not on registration/edit.html.erb (localhost:3000/users/edit)
To that end, I've set up the following in users_controller.rb
before_action :set_calc_vars, only: [:edit, :show]
def edit
@user = User.find(params[:id])
respond_to do |format|
format.html # edit.html.erb
format.xml { render :xml => @user }
end
end
def show
@user = User.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @user }
end
end
protected
#Sets value for calculated variables
def set_calc_vars
user_client = User.find(params[:id])
@rev_left = user_client.revlimit - user_client.revused
end
On both pages, the code to display the value of @rev_left is:
<%= @rev_left %>
I'm not certain why set_calc_vars is running on show.html.erb but not edit.html.erb.
On show.html.erb it displays the correct integer value for the logged in user. On edit.html.erb is displays nothing, suggesting the value is nil.
My routes are as follows:
devise_for :users
resources :users, :only => [:show]
root 'pages#home'
Upvotes: 1
Views: 3529
Reputation: 3438
According to your routes file, you use devise
for authentication and deny all the actions of users_controller
except show
:
resources :users, :only => [:show]
That means that registration/edit action is served by Devise::RegistrationsController
(give it a look here). In case you want to perform some filtering for edit
action, you should implement before_action
inside of that particular registrations_controller
. This can be achieved by a couple of ways:
devise
gem's source code. In this case you should find your gem's local folder and modify the corresponding .rb
file (NOT RECOMMENDED).devise
default controller with your own and applying your before_action
to edit
of that overriden controller. Basically you can just create folder named devise
in your controllers and copy/paste the entire devise controller into it. This requires no additional configuration. For more info on this topic you can take a look at this thread or even just watch through other topics here at Stack Overflow with "devise overriding" titles.Upvotes: 2