ruby on rails - Allowing additional parameters for admin accounts -


I have many models in which the normal user should not be able to change, but administrators should (for example) This is not my problem domain), normal users should not be able to change the post ' user_id , but administrators should be allowed to do this. / P>

It is very easy to handle at the view level - I can show the field whether a user is a administrator or not - but I am not sure how to control it in the strong parameter handling of the controller. The only solution I can come up with (and the solution), repeat yourself again, you try not to put in the train:

  def post_params If the administrator? Params.require (: post) .permit (: title ,: text ,: date ,: user_id) Other parameters.Require (: post) .permit (: title,: text, date) end end   

Is this a better way to handle?

I do not think anything wrong with your current implementation being said that, if you have a different If you want to reuse these attribute permissions in the Controller (such as an API :: Post Controller), one of the methods would be to subtly put the code in its class. This approach is used in Ryan Bates ( Note: Pro Account Required ).

  # app / model / permission_update RB category permit & lt; Struct.new (: params, user) def post if user & amp; Amp; User.admin? Params.require (: post) .permit (: title,: text, date ,: user_id) Other parameters. You request this code to be  ApplicationController  

 
> # app / controllers / application_controller.rb def can get the permission from the inside of the institute.

And then use it in any controller where you need permission logic without repeating the argument.

  # app / controller /posts_controller.rb def update @ post = post.fund (params [: id]) if @ post.update_attributes (permission_update.log) ... and ... What's the best thing about this solution is that you want to modify it by modifying the  allowed  class to dry your thoughts Can also be used. 

  # app / models / allowed_params.rb Class Permission Perm & lt; Struct.new (Parameters, User) DRF Post Params.Require (Post). Permit (* Post_Aditbut) and Diff Post_Attags if user & amp; Amp; User.admin? [: Title, Text, Date ,: user_id] and [: title,: text, date] end end end  

and permission_pagues As a visual assistant

  # app / controller / application_controller.rb def permissionPermate @ permited_prem = = Permitted Perm New (Parameters, Current_user) End Assistant: Method: Permission_param  

Finally, use it to show / hide the form field within your view.

  # app / views / posts / edit.html.erb & lt;% If Permission is granted then para_key: User_id% & gt; # User_id field & lt;% end% & gt; Show  

Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -