2

I've noticed that created resouce attributes are all blank after executing build_resource at Devise::RegistrationsController create function

here is the params object sent:

 "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"=>"gion@gmail.com",
 "password"=>"[FILTERED]",
 "password_confirmation"=>"[FILTERED]",
 "country_id"=>"2",
 "state_province_id"=>"2"}

Here is the resulted resource attributes:

[["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", ""]]

So, why build_resource could not populate the params ? Where to look to solve this problem ? Any help will be more than appreciated.

EDIT:

Here is the student model:

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

The student model in route file:

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

EDIT2:

Here is the student registration controller:

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
simo
  • 23,342
  • 38
  • 121
  • 218
  • Please add the code that shows the call to the `build_resource` method. – Thilo Feb 08 '12 at 07:43
  • its the create method of Devise::RegistrationsController which is called when a user submit the new user form – simo Feb 08 '12 at 07:57
  • So you're not using Devise's registration_controller but your own. Post that code too. – Thilo Feb 08 '12 at 12:39
  • Thanks Thilo, Actually I am extending Devise's registration_controller, please see EDIT2 above – simo Feb 08 '12 at 20:37

3 Answers3

16

I had a similar problem. In my create method build_resource didn't took the given parameters and registration failed. -> Email can't be blank message

The problem was that i am using Devise 3.0 and that worked for me:

build_resource sign_up_params

Devise < 3.0 works without sign_up_params pretty well

Chriz
  • 206
  • 2
  • 7
  • The `build_resource` method became less magic after 3.x - previously it selected `params[resource_name]` as the default hash to supply the resource building. – ocodo Oct 23 '13 at 01:30
  • Just sending in params would not work, You need to specifiy the resource name explicitely.... Say for Model named Student, build_resource params[:student] will send in the params. @Chriz thanks a ton mate....I ve been sitting with this problem for 4 straight hours finally got the answer here.... – Sri Murthy Upadhyayula Nov 20 '13 at 09:28
1

Well, the build_resource method looks like this:

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

So it looks like it's returning a resource built from an empty hash, which in turn might indicate a naming issue. Post your relevant section from the routes file please and the Student model (the devise: and devise_for parts).

Thilo
  • 17,565
  • 5
  • 68
  • 84
  • Thanks Thilo, unfortunately, I have the code at home, as soon as I return home, I will post the code you required, but, how can I alert you then ? – simo Feb 08 '12 at 08:45
  • Please see the EDIT above, notice that the client side validations do not work when submitting the form, I hope this will help. – simo Feb 08 '12 at 11:52
  • Please note: in Devise 3.x `build_resource` does not assume that `hash` is `params[resource_name]` if not supplied, and `params[resource_name]` should be explicitly supplied. – ocodo Oct 23 '13 at 01:27
  • @ocodo `self.resource = resource_class.new_with_session(hash || {}, session)` it should be set as session right? – Fabrizio Bertoglio Jul 06 '17 at 16:19
1

if I remove this line from my Student model:

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

every thing work fine!, I shouldn't add attr_accessor , if I add it, devise will not be able to create a new resource from sent parameters.

simo
  • 23,342
  • 38
  • 121
  • 218