vendredi 29 mai 2015

Rails Trying to access perameters in Model

I've created a User model through Devise gem

My relationships are setup as:

  • The User has a many to many relationship with Role
  • The User belongs_to (one to many relationship with) Batch
  • The User has_one (one to one relationship with) UserProfile

Now everytime a User is created I am calling the method cup that creates a UserProfile as well. The problem is that I can't access anything other then devise's own perameters in the Model.

For example I have a drop down to select for Role but i cant access its selected value

My aim is to do something like:

  def cup
    if role.title == 'this'
      create_user_profile(:username => 'username01')
    else
      create_user_profile(:username => 'username02')
    end
  end

This is what the model looks like:

class User < ActiveRecord::Base

  has_and_belongs_to_many :roles
  belongs_to :batch
  has_one :user_profile

  after_create :cup

  def cup
    create_user_profile(:username => 'username')

  end

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

My parameters look like this:

{"utf8"=>"✓", "authenticity_token"=>"TbsLJxZB2aeDgj8RitTRKURCc3KODCKpv/xphwhAaIw=", "users"=>{"username"=>"rfdsfew", "role_id"=>"1", "batch_id"=>"1"}, "user"=>{"email"=>"2@223.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}

I can access the params in user hash but not in users hash

My guess is that, this is because the users hash is not allowed in devise controller, since I can't access devise's controller I have no idea how to permit the users hash.

And I found this on the internet but it didn't work:

before_filter :configure_permitted_parameters, if: :devise_controller?

protected

def configure_permitted_parameters
  devise_parameter_sanitizer.for(:sign_up) << :username
end

Aucun commentaire:

Enregistrer un commentaire