Ruby on rails 添加设计自定义字段时用户的未允许参数
我有两类用户:普通用户和专业用户 Pros是用户,但在名为Ruby on rails 添加设计自定义字段时用户的未允许参数,ruby-on-rails,devise,Ruby On Rails,Devise,我有两类用户:普通用户和专业用户 Pros是用户,但在名为:Pros的单独表中有额外字段 因此,我为:pro做了一个单独的注册表,其中包括:users字段,并为添加了一个字段,用于新的:pro字段 我还将这些新参数添加到应用程序控制器中,以便Desive允许它们 提交注册表时,会创建用户,但我的日志中出现以下错误: Started POST "/users" for 127.0.0.1 at 2014-11-13 00:53:43 +0100 Processing by Registration
:Pros
的单独表中有额外字段
因此,我为:pro
做了一个单独的注册表,其中包括:users字段,并为添加了一个字段,用于新的:pro
字段
我还将这些新参数添加到应用程序控制器中,以便Desive允许它们
提交注册表时,会创建用户,但我的日志中出现以下错误:
Started POST "/users" for 127.0.0.1 at 2014-11-13 00:53:43 +0100
Processing by RegistrationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"zUVLJFHhShoHvUVneGNmCf46E4KPWaINeTw4o7iCa7w=", "user"=>{"name"=>"asdasd", "email"=>"asdasd@sss.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "pros"=>{"type"=>"Marca de Decoración", "web"=>"asadasd", "telephone"=>"765876", "about"=>"sadasd"}, "tos_agreement"=>"1"}, "commit"=>"Registrarme y aplicar para PRO"}
Unpermitted parameters: pros
我的观点是:
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= f.label :name, "Nombre de usuario" %>
<%= f.text_field :name, :autofocus => true %>
<%= f.label :email %>
<%= f.email_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %>
<%= f.fields_for :pro do |pro| %>
<%= pro.select :type,["Marca de Decoración","Tienda de Decoración","Blogger"] %>
<%= pro.text_field :web, placeholder: "http://www.miweb.com" %>
<%= f.label :telephone, "Teléfono" %>
<%= pro.text_field :telephone, placeholder: "Teléfono", label: "Teléfono de contacto" %>
<%= pro.text_field :about%>
<% end %>
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:name, :tos_agreement, :avatar, :avatar_cache, :email, :password, :password_confirmation, pros_attributes: [:pro_name, :pro_image, :is_active, :web, :user_id, :about, :facebook, :twitter, :linkedin, :telephone]) }
end
我的模型关系:
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= f.label :name, "Nombre de usuario" %>
<%= f.text_field :name, :autofocus => true %>
<%= f.label :email %>
<%= f.email_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %>
<%= f.fields_for :pro do |pro| %>
<%= pro.select :type,["Marca de Decoración","Tienda de Decoración","Blogger"] %>
<%= pro.text_field :web, placeholder: "http://www.miweb.com" %>
<%= f.label :telephone, "Teléfono" %>
<%= pro.text_field :telephone, placeholder: "Teléfono", label: "Teléfono de contacto" %>
<%= pro.text_field :about%>
<% end %>
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:name, :tos_agreement, :avatar, :avatar_cache, :email, :password, :password_confirmation, pros_attributes: [:pro_name, :pro_image, :is_active, :web, :user_id, :about, :facebook, :twitter, :linkedin, :telephone]) }
end
User.rb
has_one :pro
accepts_nested_attributes_for :pro, allow_destroy: true
Pro.rb
has_one :pro
accepts_nested_attributes_for :pro, allow_destroy: true
属于:用户
我的应用程序控制器:
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= f.label :name, "Nombre de usuario" %>
<%= f.text_field :name, :autofocus => true %>
<%= f.label :email %>
<%= f.email_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %>
<%= f.fields_for :pro do |pro| %>
<%= pro.select :type,["Marca de Decoración","Tienda de Decoración","Blogger"] %>
<%= pro.text_field :web, placeholder: "http://www.miweb.com" %>
<%= f.label :telephone, "Teléfono" %>
<%= pro.text_field :telephone, placeholder: "Teléfono", label: "Teléfono de contacto" %>
<%= pro.text_field :about%>
<% end %>
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:name, :tos_agreement, :avatar, :avatar_cache, :email, :password, :password_confirmation, pros_attributes: [:pro_name, :pro_image, :is_active, :web, :user_id, :about, :facebook, :twitter, :linkedin, :telephone]) }
end
尝试将您的控制器更改为此,以便生成一个新的pro实例,并且您的表单助手应该生成pro\u属性
。我假设这是一个新动作,所以你不需要对你的用户调用“创建”,而是“新建”。可能您需要在允许的参数方法中将pro\u属性
更改为pro\u属性
,因为它有一个关系
def pro_new
@user = User.new
@user.build_pro
render "devise/registrations/new-pro-registration"
end
希望这能奏效。我可以给你一个快速的答案,但我认为你的设计需要一些工作。使用和使用以下格式(特别是从长远来看)会更好
User#(基类)
有一个:userInfo
委托:列出\u pro\u字段,收件人::userInfo
基本用户
虽然您可以将Pro
和ProInfo
之间的关系放在基类上,但如果用户能够丢失(并重新获得)其Pro状态,我会将其保留在基类上。这将防止ex-Pros用户信息中出现孤立记录
这也将导致更好的数据分离和响应时间,因为您不需要很多时间的许多字段都被移动到一个单独的表中,可以根据需要进行连接。我完全同意@smallbutton.com
您需要更改专业属性,而不是专业属性。您可以使用params.require(:user.permit)!如果您想接受所有参数。您拥有的关联与用户拥有的关联类似。
然后您已经在允许的参数中传递了数组,但它是一个关系
应该是这样的
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:name, :tos_agreement, :avatar, :avatar_cache, :email, :password, :password_confirmation, pro_attributes: (:pro_name, :pro_image, :is_active, :web, :user_id, :about, :facebook, :twitter, :linkedin, :telephone)) }
end
它将起作用:)检查控制器中pro和pro的单数和复数部分