关于ruby:rails 3.1,为什么设计无法构建资源?

rails 3.1, why devise can't build the resource?

我注意到在Devise::RegistrationsControllercreate函数上执行build_resource后,创建的resource属性都是空白的。

以下是发送的params对象:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
"student"=>{"first_name"=>"George",
"last_name"=>"Michle",
"company_name"=>"Meri",
"work_title"=>"Architect",
"address"=>"Moon",
"postal_code"=>"23410",
"work_phone"=>"",
"home_phone"=>"",
"mobile_phone"=>"",
"fax"=>"",
"email"=>"[email protected]",
"password"=>"[FILTERED]",
"password_confirmation"=>"[FILTERED]",
"country_id"=>"2",
"state_province_id"=>"2"}

以下是结果资源属性:

1
[["address",""], ["city", nil], ["company_name",""], ["country_id", 1], ["created_at", Wed, 08 Feb 2012 04:52:35 UTC +00:00], ["current_sign_in_at", nil], ["current_sign_in_ip", nil], ["email",""], ["encrypted_password",""], ["fax",""], ["first_name",""], ["home_phone",""], ["last_name",""], ["last_sign_in_at", nil],  ["last_sign_in_ip", nil], ["license_number", nil], ["mobile_phone",""], ["postal_code",""], ["remember_created_at", nil], ["reset_password_sent_at", nil], ["reset_password_token", nil], ["sign_in_count", 0], ["state_province_id", nil], ["updated_at", Wed, 08 Feb 2012 04:52:35 UTC +00:00], ["username", nil], ["work_phone",""], ["work_title",""]]

那么,为什么构建资源不能填充参数?在哪里解决这个问题?任何帮助都将不胜感激。

编辑:

学生模型如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
class Student < ActiveRecord::Base

  has_many :students_courseses
  has_many :courses , :through => :students_courseses



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

  belongs_to :country
  #validates :weburl, :url => {:allow_blank => true}, :length => { :maximum => 50 }

  validates_length_of :work_phone, :is => 10, :message => 'must be 10 digits, excluding special characters such as spaces and dashes. No extension or country code allowed.', :if => Proc.new{|o| !o.work_phone.blank?}
  validates_length_of :home_phone, :is => 10, :message => 'must be 10 digits, excluding special characters such as spaces and dashes. No extension or country code allowed.', :if => Proc.new{|o| !o.home_phone.blank?}
  validates_length_of :mobile_phone, :is => 10, :message => 'must be 10 digits, excluding special characters such as spaces and dashes. No extension or country code allowed.', :if => Proc.new{|o| !o.mobile_phone.blank?}
  validates_length_of :fax, :is => 10, :message => 'must be 10 digits, excluding special characters such as spaces and dashes. No extension or country code allowed.', :if => Proc.new{|o| !o.fax.blank?}

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me

  attr_accessible:username
  attr_accessible:first_name
  attr_accessible:last_name
  attr_accessible:address
  attr_accessible :city

  attr_accessible :state_province_id
  attr_accessible :country_id

  attr_accessible:postal_code
  attr_accessible:work_phone
  attr_accessible:home_phone
  attr_accessible:mobile_phone
  attr_accessible:fax

  attr_accessible:company_name
  attr_accessible:work_title
  attr_accessible:license_number

  ###############################################################


  #attr_accessor can be used for values you don't want to store in the database directly and that will only exist for the life of the object (e.g. passwords).
  attr_accessor :email, :password, :password_confirmation, :remember_me
  ###############################################################


  cattr_accessor :time_out_time

  def timeout_in
    if time_out_time.nil?
      STUDENT_LOG_OUT_PERIOD
    else
      time_out_time.seconds
    end
  end
end

路径文件中的学生模型:

1
2
  devise_for :students, :controllers => { :registrations =>"students/registrations" }
  devise_for :students, :controllers => { :sessions =>"students/sessions" }

编辑2:

这是学生注册管理员:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Students::RegistrationsController < Devise::RegistrationsController

  def update
    # no mass assignment for country_id, we do it manually
    # check for existence of the country in case a malicious user manipulates the params (fails silently)
    if student_signed_in? then
      if params[resource_name][:country_id]
        resource['country_id'] = params[resource_name][:country_id] if Country.find_by_id(params[resource_name][:country_id])
      end
      if params[resource_name][:state_province_id]
        resource.state_province_id = params[resource_name][:state_province_id] if State.find_by_id(params[resource_name][:state_province_id])
      end
    end
    super
  end

  def create
    super
  end

end


我也有类似的问题。在我的创建方法中,构建资源没有使用给定的参数,注册失败。->电子邮件不能为空邮件

问题是我使用的是Designe3.0,这对我很有用:

1
build_resource sign_up_params

device<3.0在没有注册参数的情况下运行良好


如果我从学生模型中删除此行:

1
attr_accessor :email, :password, :password_confirmation, :remember_me

一切正常!,我不应该添加attr_accessor,如果我添加它,design将无法从发送的参数创建新的资源。


嗯,构建资源方法如下:

1
2
3
4
def build_resource(hash=nil)
  hash ||= params[resource_name] || {}
  self.resource = resource_class.new_with_session(hash, session)
end

所以它看起来像是返回了一个从空哈希构建的资源,而这又可能指示一个命名问题。请在Routes档案和学生模型(devise:devise_for部分)中张贴您的相关章节。