Rails 4.0 与 Devise。嵌套属性 不允许的参数

2024-04-25

我正在使用 Devise 和 Rails 4 开发一个网络应用程序。我有一个User我用 2 个额外的表单字段扩展了该模型,这样当用户注册时,他还可以提交他的名字/姓氏。 (基于http://blog.12spokes.com/web-design-development/adding-custom-fields-to-your-devise-user-model-in-rails-4/ http://blog.12spokes.com/web-design-development/adding-custom-fields-to-your-devise-user-model-in-rails-4/)。我现在想添加一个机构模型。这个型号has_many:用户和用户属于:机构。我希望能够注册机构名称在同一张表格上我注册了用户。我知道我需要一个nested_attribute机构模型,因为这是父级,我稍后会展示它。当我尝试注册用户时,我进入控制台:不允许的参数:机构.

我的提示是,我无法根据我的子类(用户)更新我的父类(机构)。这个问题可能有解决办法吗?或者有人经历过类似的事情吗?

class Institutions < ActiveRecord::Base
    has_many :users, 
    accepts_nested_attributes_for :users
end

class User < ActiveRecord::Base
     devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable
     belongs_to :institution
end

注册/new.html.erb这里我有嵌套的形式

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f|     %>
<%= devise_error_messages! %>
.
. 
    <%= f.fields_for :institutions do |i| %>
        <p><%= i.label :name %><br />
        <%= i.text_field :institutions_attr %></p>
    <% end %>

根据我之前链接的教程,我创建了一个新的用户::参数消毒器它继承自Devise::参数消毒剂并覆盖sign_up方法如下:

lib/user_sanitizer.rb

private
def sign_up
    default_params.permit(:first_name, :last_name ,:email, :password,  :password_confirmation, :current_password, institutions_attributes: [:id, :name])
end

最后,我的应用程序控制器.rb

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception

  protected
  def devise_parameter_sanitizer
    if resource_class == User
    User::ParameterSanitizer.new(User, :user, params)
    else 
    super
    end
  end
end

感谢您的阅读!

控制台参数输出:

{"utf8"=>"✓",
 "authenticity_token"=>"JKuN6K5l0iwFsj/25B7GKDj7WEHR4DO3oaVyGxGJKvU=",
 "user"=>{"email"=>"[email protected] /cdn-cgi/l/email-protection",
 "first_name"=>"abc",
 "last_name"=>"xyz",
 "institutions"=>{"name"=>"Government"},
 "password"=>"[FILTERED]",
 "password_confirmation"=>"[FILTERED]"},
 "commit"=>"Sign up"}

EDIT

根据建议,我添加了

params.require(resource_name).permit( :email, :first_name, :last_name, institution:  [:name], :password, :password_confirmation ) and I get an *error syntax error, unexpected ',', expecting => ...nstitution: [:name], :password, :password_confirmation )*

但是,如果我重新编辑

params.require(resource_name).permit( :email, :first_name, :last_name, :password, :password_confirmation, institution:  [:name] ) 

我没有收到任何语法错误,但收到了 Unpermitedparameters:Institutions in the Request。

我相信发生这种情况是因为用户是机构的子级。但是,我一直无法找到解决此问题的方法。


配置/routes.rb

像这样创建您自己的注册控制器...(请参阅此处的设计文档以了解覆盖控制器的详细信息... https://github.com/plataformatec/devise#configuring-controllers) ...这是比通过ApplicationController

devise_for :users, controllers: {registrations: 'users/registrations'}

应用程序/控制器/用户/registrations_controller.rb

重写新方法来创建Profile与相关的User模型如下...运行configure_permitted_parameters方法之前清理参数(注意如何添加嵌套参数)

class Users::RegistrationsController < Devise::RegistrationsController

  before_filter :configure_permitted_parameters

  # GET /users/sign_up
  def new

    # Override Devise default behaviour and create a profile as well
    build_resource({})
    resource.build_profile
    respond_with self.resource
  end

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) { |u|
      u.permit(:email, :password, :password_confirmation, :profile_attributes => :fullname)
    }
  end
end

db/migrate/xxxxxxxxxxxxxx_create_profiles.rb

这是生成的迁移Profile model (请注意参考User) ...这个示例配置文件只保留fullname作为一个延伸User但请随意添加!

class CreateProfiles < ActiveRecord::Migration
  def change
    create_table :profiles do |t|
       t.references :user
       t.string :fullname
       t.timestamps
    end
  end
end

应用程序/模型/user.rb

class User < ActiveRecord::Base

  # Associations
  has_one :profile, dependent: :destroy, autosave: true

  # Allow saving of attributes on associated records through the parent,
  # :autosave option is automatically enabled on every association
  accepts_nested_attributes_for :profile

  # Devise
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
end

应用程序/模型/profile.rb

class Profile < ActiveRecord::Base

  # Associations
  belongs_to :user

  # Validations
  validates :fullname, presence: true
end

应用程序/视图/设计/注册/new.html

<% resource.build_profile if resource.profile.nil? %>
<%= form_for(resource, :as => resource_name,
                       :url => registration_path(resource_name)) do |f| %>
  <ul>

    <%= devise_error_messages! %>

    <li class="fullname">
      <%= f.fields_for :profile do |profile_fields| %>
        <%= profile_fields.label :fullname %>
        <%= profile_fields.text_field :fullname %>
      <% end %>
    </li>
    <li class="email">
      <%= f.label :email %>
      <%= f.email_field :email, :autofocus => true %>
    </li>
    <li class="password">
      <%= f.label :password %>
      <%= f.password_field :password %>
    </li>
    <li class="password">
      <%= f.label :password_confirmation %>
      <%= f.password_field :password_confirmation %>
    </li>
    <li>
      <%= f.submit %>
    </li>
    <li>
      <p><%= render "devise/shared/links" %></p>
    </li>
  </ul>
<% end %>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Rails 4.0 与 Devise。嵌套属性 不允许的参数 的相关文章

随机推荐