0

我正在尝试使用设计构建一个应用程序,该应用程序提供一个登录和注销界面,但多个注册界面取决于要注册的模型。因此,我遇到了非常适合我需要的多态关联,因为我想为不同的模型提供单独的表。

但是我无法注册任何用户,因为我总是在基本用户模型的属性方面遇到一些错误。我已经阅读了https://stackoverflow.com/a/11956734/1183192https://stackoverflow.com/a/15459313/1183192,我想我已经适应了一切。

基本用户模型:

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :role_id, :role_type,
                  :salutation, :firstname, :lastname, :street, :zipcode, :city, :phone

  belongs_to :role, polymorphic: true

  def customer?
    'Customer' == self.role_type
  end

  def customer
    Customer.find(self.role_id) if self.customer?
  end

  def admin?
    'Admin' == self.role_type
  end

  def admin
    Admin.find(self.role_id) if self.admin?
  end
end

客户型号:

class Customer < ActiveRecord::Base
  has_one :user, as: :role
  attr_accessible :user_attributes
  accepts_nested_attributes_for :user
end

客户控制器:

class CustomersController < ApplicationController
  # removed all methods except new and create

  def new
    @customer = Customer.new
    @customer.build_user

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @customer }
    end
  end

  def create
    @customer = Customer.new(params[:customer])

    respond_to do |format|
      if @customer.save
        format.html { redirect_to @customer, notice: 'Customer was successfully created.' }
        format.json { render json: @customer, status: :created, location: @customer }
      else
        format.html { render action: "new" }
        format.json { render json: @customer.errors, status: :unprocessable_entity }
      end
    end
  end
end

查看(new.html.erb):

<h1>New customer</h1>

<%= form_for @customer do |f| %>

    <%= f.fields_for @customer.user do |u| %>

      <div class="row">
        <div class="small-12 large-12 columns">
          <%= u.label :email %>
          <%= u.email_field :email, required: true, autofocus: true %>
        </div>
      </div>
      <div class="row">
        <div class="small-12 large-12 columns">
          <%= u.label :password %>
          <%= u.password_field :password, required: true %>
        </div>
      </div>
      <div class="row">
        <div class="small-12 large-12 columns">
          <%= u.label :password_confirmation %>
          <%= u.password_field :password_confirmation, required: true %>
        </div>
      </div>
      <div class="row">
        <div class="small-12 large-12 columns">
          <%= u.label :salutation %>
          <%= u.text_field :salutation %>
        </div>
      </div>
      <div class="row">
        <div class="small-12 large-12 columns">
          <%= u.label :firstname %>
          <%= u.text_field :firstname %>
        </div>
      </div>
      <div class="row">
        <div class="small-12 large-12 columns">
          <%= u.label :lastname %>
          <%= u.text_field :lastname %>
        </div>
      </div>
      <div class="row">
        <div class="small-12 large-12 columns">
          <%= u.label :street %>
          <%= u.text_field :street %>
        </div>
      </div>
      <div class="row">
        <div class="small-12 large-12 columns">
          <%= u.label :zipcode %>
          <%= u.text_field :zipcode %>
        </div>
      </div>
      <div class="row">
        <div class="small-12 large-12 columns">
          <%= u.label :city %>
          <%= u.text_field :city %>
        </div>
      </div>
      <div class="row">
        <div class="small-12 large-12 columns">
          <%= u.label :phone %>
          <%= u.text_field :phone %>
        </div>
      </div>

    <% end %>

    <%= f.submit class: "button success" %>
<% end %>

新页面呈现正确,但是当我尝试提交时,出现以下错误:

ActiveModel::MassAssignmentSecurity::Error in CustomersController#create
Can't mass-assign protected attributes: user

参数是:

{"utf8"=>"✓",
 "authenticity_token"=>"WngmLBR7MmVuhDyYxsASWQscUTB/VFmKmn/fdatVB30=",
 "customer"=>{"user"=>{"email"=>"max@mustermann.com",
 "password"=>"[FILTERED]",
 "password_confirmation"=>"[FILTERED]",
 "salutation"=>"",
 "firstname"=>"Max",
 "lastname"=>"Mustermann",
 "street"=>"Musterstraße 1",
 "zipcode"=>"12345",
 "city"=>"Musterstadt",
 "phone"=>"4917620289872"}},
 "commit"=>"Create Customer"}

我认为错误出现在 Customer 类中,但我不知道如何修复它。

更新

我对客户模型进行了更改:

class Customer < ActiveRecord::Base
  has_one :user, as: :role
  attr_accessible :user # :user instead of :user_attributes
  accepts_nested_attributes_for :user
end

现在我收到以下错误:

ActiveRecord::AssociationTypeMismatch in CustomersController#create
User(#70271634657140) expected, got ActiveSupport::HashWithIndifferentAccess(#70271609254480)

app/controllers/customers_controller.rb:44:in `new'
app/controllers/customers_controller.rb:44:in `create'
4

2 回答 2

1

把你的观点:

f.fields_for :user do |u|
#...

代替

f.fields_for @customer.user do |u|

发生错误是因为嵌套属性根应该是“user_attributes”,而不是“user”。

于 2013-06-11T13:44:24.623 回答
0

可能会帮助您替换 attr_accessible :user_attributes To attr_accessible :user

在那个客户模型中

更好的方法:---

application.rb 有 config.active_record.whitelist_attributes = true

请参阅批量分配保护

提前感谢

于 2013-06-11T13:31:12.437 回答