[英]Clearance additional strong_parameters in RoR4
I am using the clearance gem, which is basically an authentication by e-mail. 我使用的是clear gem,它基本上是通过电子邮件进行的身份验证。
Now I want to keep the log in via email, but want to add a 'name' field into the registration form for the user. 现在,我想通过电子邮件保持登录状态,但是想在用户的注册表单中添加一个“名称”字段。
User.rb User.rb
class User < ActiveRecord::Base
include Clearance::User
validates_presence_of :name
validates_uniqueness_of :name
end
users_controller.rb users_controller.rb
class UsersController < Clearance::UsersController
def create
@user = user_from_params
if @user.save(permit_params)
sign_in @user
render :json => {:success => true}
else
render :json => {:success => false}
end
end
private
def user_from_params
user_params = params[:user] || Hash.new
email = user_params.delete(:email)
password = user_params.delete(:password)
Clearance.configuration.user_model.new(user_params).tap do |user|
user.email = email
user.password = password
end
end
def permit_params
params.require(:user).permit(:name, :email, :encrypted_password, :password, :confirmation_token, :remember_token)
end
end
As you can see I added the permission to .save
and still the app throws me the following error upon request: 如您所见,我向.save
添加了权限,但应用程序仍根据请求向我抛出以下错误:
ActiveModel::ForbiddenAttributesError in Clearance::UsersController#create
If I remove the :name
text field from my form everything works, but I want additional fields for my form. 如果我从表单中删除:name
文本字段,则一切正常,但是我想为表单添加其他字段。
You need to put your users_controller into folders like this: 您需要将users_controller放入如下文件夹:
app/controllers/clearance/users_controller.rb 应用程序/控制器/间隙/ users_controller.rb
Then in users_controller.rb , instead of: 然后在users_controller.rb中 ,而不是:
class UsersController < Clearance::UsersController
you need to put: 你需要把:
class Clearance::UsersController < ApplicationController
This doesn't override the Clearance User Controller. 这不会覆盖“清除用户控制器”。 Instead, now you are writing it. 相反,现在您正在编写它。 This is the controller that your sign-up form will go to. 这是您的注册表单将转到的控制器。 Thus, you still need to include the base code from Clearance's gem and edit Clearance::UsersController#user_from_params 因此,您仍需要包含Clearance的gem中的基本代码并编辑Clearance :: UsersController#user_from_params
def user_from_params
user_params = params[:user] || Hash.new
email = user_params.delete(:email)
password = user_params.delete(:password)
name = user_params.delete(:name)
Clearance.configuration.user_model.new(user_params).tap do |user|
user.email = email
user.password = password
user.name = name
end
end
and update Clearance::UsersController#permit_params 并更新Clearance :: UsersController#permit_params
def permit_params
params.require(:user).permit(:name, :email, :password)
end
UPDATE: I've changed my previous answer since it was missing the point. 更新:我已经改变了以前的答案,因为它没有指出重点。
The code you've provided is fine. 你提供的代码很好。 The reason you're getting ActiveModel::ForbiddenAttributesError
is because you're not overriding the Clearance Users Controller. 您收到ActiveModel::ForbiddenAttributesError
的原因是因为您没有覆盖Clearance Users Controller。
You need to tell your app to use your overriding controller instead of the controller inside Clearance's engine, by adding the following to your config/routes.rb. 您需要通过将以下内容添加到config / routes.rb中,来告诉您的应用使用替代控制器,而不是Clearance引擎内部的控制器。
resources :users,
controller: 'users',
only: 'create'
IMO this is better than writing a new Clearance::UsersController
in your application code above. IMO这比在上面的应用程序代码中编写一个新的Clearance::UsersController
更好。
I tried both ways and came to the realization that you don't need the permit params. 我尝试了两种方式,然后意识到您不需要许可证参数。 You only need to do 4 things: 你只需要做4件事:
Starting from the generated migration rails generate clearance:install
(before running rake db:migrate
) 从生成的迁移rails generate clearance:install
(在运行rake db:migrate
)
add a name:string
column with an index such so that your migration looks like: by adding this column, it is included in the creation of the user params. 添加具有索引的name:string
列,以便您进行迁移,如下所示:通过添加此列,它包含在用户参数的创建中。
def change change_table :users do |t| t.timestamps null: false t.string :email, null: false t.string :name, null: false, limit: 50 t.string :encrypted_password, limit: 128, null: false t.string :confirmation_token, limit: 128 t.string :remember_token, limit: 128, null: false end add_index :users, :name add_index :users, :email add_index :users, :remember_token end
Then you only need to create 2 files: 然后,您只需要创建2个文件:
app/controllers/users_controller.rb 应用程序/控制器/ users_controller.rb
class UsersController < Clearance::UsersController
def create
@user = user_from_params
if @user.save
sign_in @user
redirect_to '/'
else
render template: 'users/new'
end
end
private
def user_from_params
user_params = params[:user] || Hash.new
name = user_params.delete(:name)
email = user_params.delete(:email)
password = user_params.delete(:password)
Clearance.configuration.user_model.new(user_params).tap do |user|
user.name = name
user.email = email
user.password = password
end
end
end
and 和
app/views/users/new.html.erb 应用程序/视图/用户/ new.html.erb
<div id='clearance' class='sign-up'>
<h2><%= t('.title') %></h2>
<%= form_for @user do |form| %>
<div class='text-field'>
<%= form.label :name %>
<%= form.text_field :name, :type => 'name' %>
</div>
<div class='text-field'>
<%= form.label :email %>
<%= form.text_field :email, :type => 'email' %>
</div>
<div class='password-field'>
<%= form.label :password %>
<%= form.password_field :password %>
</div>
<div class='submit-field'>
<%= form.submit %>
</div>
<div class='other-links'>
<%= link_to t('.sign_in'), sign_in_path %>
</div>
<% end %>
</div>
Then your routes should include this 然后你的路线应该包括这个
resources :users,
controller: 'users',
only: 'create'
You actually don't need to reimplement create
method, just in case if you're creating an API (what looks like). 实际上,您不需要重新实现create
方法,以防万一您正在创建API(看起来像这样)。
The steps are: 步骤是:
config/routes.rb 配置/ routes.rb中
Rails.application.routes.draw do resources :users, controller: 'users', only: Clearance.configuration.user_actions end
app/controllers/users_controller.rb 应用程序/控制器/ users_controller.rb
class UsersController < Clearance::UsersController private def user_from_params email = user_params.delete(:email) password = user_params.delete(:password) name = user_params.delete(:name) Clearance.configuration.user_model.new(user_params).tap do |user| user.email = email user.password = password user.name = name end end end
After that, add the name field into your form and it's done. 之后,将名称字段添加到表单中并完成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.