ruby on rails - before_action Not Running Before Edit Action -
I want to make some calculations based on the integer values in the database of "user" class. (Using Devise.) This users / show.html ARB , but not Registration / edit.html.erb (localhost: 3000 / user / editing)
For this, I Set up in users_controller.rb
before_action: set_calc_vars, only: [: edit,: show] edit df @username = user. FIDD (Param [: ID]) Answer_to do dot | Format | Format.html # edit.html.erb format.xml {Render: xml = & gt; @ User} End and show show @ user = user.fid (param [: id]) answer_to do it. Format | Format.html # show.html.erb format.xml {Render: xml = & gt; @User} End and Protected # Defined set defaults to set_calc_vars user_client = User.find (params [: id]) @rev_left = user_client.revlimit - user_client.revused End Pages on both, the code to display the value of @rev_left is:
<% = @ rev_left% & gt; I do not know why set_calc_vars is running on show.html.erb but not edit.html.erb
log on to show.html.erb Displays the correct integer value for the user.
devise_for: user resource: user, only => Edit. Does not show anything on ARB, it is suggested that the value is zero. ; [: Show] Route 'Page # Home'
According to your route file, you Use devise for authentication and show :
resources: users, except: users_controller Reject all tasks only = & gt; [: Show]
This means that Registration / Editing action by DESIS :: Registration Controller (give this a look) She goes. If you want to do some filtering for the edit operation, then you should implement the before_action inside that particular registration_controller . This can be achieved in a number of ways:
- Monkey-patching
devisesource code in the gem In this case you should find the local folder of your gem and the related.rbshould modify the file (not recommended). - Override the
devisedefault controller with your code and implementing itsbefore_actiontoeditthat override controller Actually, you can simply create the name folder ofdevisein your controllers and copy / paste the entire device controller into it. There is no additional configuration required for this. For more information on this topic, you can see or even see the "override" title along with other topics on stack overflow.
Comments
Post a Comment