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'