简体   繁体   English

Rails 4设计强参数管理模型

[英]Rails 4 Devise Strong Parameters Admin Model

I have made a user and admin model using devise. 我已经使用devise建立了用户和管理员模型。 I have used strong parameters in the app/controllers/application_controller.rb file 我在app / controllers / application_controller.rb文件中使用了强大的参数

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
  before_filter :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password, :password_confirmation, :remember_me) }
    devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:login, :password, :remember_me) }
  end

end

How do I whitelist the admin model? 如何将管理员模型列入白名单?

devise_parameter_sanitizer.for(:sign_in) { |a| a.permit(:login, :password, :remember_me) }

Also how do I whitelist the admin sign_up so that no variables may be passed into it? 另外,我该如何将管理员sign_up列入白名单,以免将任何变量传递给它? My guess is 我的猜测是

devise_parameter_sanitizer.for(:sign_up) { |a| a.permit()}

UPDATE UPDATE

I would like to edit my question. 我想编辑我的问题。

My question is how do I get the admin model to automatically blacklist my admin sign up page? 我的问题是如何使管理员模型自动将我的管理员注册页面列入黑名单? If I simply leave nothing then I can still sign up through the "admins/sign_up" . 如果我什么都没留下,那么我仍然可以通过“ admins / sign_up”进行注册 Sure I can delete the :regisitrations within the "app/models/admin.rb" , but I would like to deny command line sign ups 当然,我可以删除“ app / models / admin.rb”中的:regisitrations,但是我想拒绝命令行注册

--Would it be wise to use scoped views and specifically define each view for the admin and user models?-- -使用作用域视图并专门为管理员和用户模型定义每个视图是否明智?-

If you have separate controllers for users and admins then try something like this: 如果您为用户和管理员使用单独的控制器,请尝试如下操作:

def configure_permitted_parameters
  if params[:controller] == "user"
    devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password, :password_confirmation, :remember_me) }
    devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:login, :password, :remember_me) }
  else
    devise_parameter_sanitizer.for(:sign_in) { |a| a.permit(:login, :password, :remember_me) }
    devise_parameter_sanitizer.for(:sign_up) { |a| a.permit()}

and there's another approach to it as well, create only one controller for both users and admin, named registrations_controller.rb, have a field in your users table named is_admin and set it for a user only if he/she is a admin. 还有另一种方法,只为用户和管理员创建一个名为registrations_controller.rb的控制器,在用户表中有一个名为is_admin的字段,并仅当他/她是管理员时才为用户设置该控制器。 Create a seed to make your admins like this in your seeds.rb file: 在Seeds.rb文件中创建一个种子,使您的管理员像这样:

admin_user = User.new(email: "abc@xyz.com", password: "123", password_confirmation: "123", is_admin: "true" )
admin_user.skip_confirmation!
admin_user.save

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM