0

When a new users comes to our site they can sign up as a regular user or an employer. I have a boolean field employer that can be checked either true or false when a new user signs up. The problem I am having is I can't seem to save (on sign up) or change (on update) the default value from false to true via the form. I have already research this question very well before asking and when I do rails c

2.0.0-p353 :010 > u = User.first
  User Load (0.4ms)  SELECT  "users".* FROM "users"   ORDER BY "users"."id" ASC LIMIT 1 => #
  <User id: 1, email: "club@test.com", encrypted_password: "$2a$10$7Tfjwbj1PXvNpmiLjCFXj.VuaTVcMqcsRmeffGJWfXa...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 2, current_sign_in_at: "2014-05-12 15:40:01", last_sign_in_at: "2014-05-12 15:15:51", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", created_at: "2014-05-12 15:15:51", updated_at: "2014-05-12 16:16:26", employer: false> 
2.0.0-p353 :011 > u.employer = true
  => true 
2.0.0-p353 :012 > u.save
  (0.1ms)  begin transaction
  SQL (0.3ms)  UPDATE "users" SET "employer" = ?, "updated_at" = ? WHERE "users"."id" = 1    [["employer", "t"], ["updated_at", "2014-05-12 16:17:04.342032"]]
  (5.9ms)  commit transaction
  => true 

I am able to change the employer boolean from the default false to true. I am pretty sure it is just an issue with the form. Any guess on how I would correctly implement a radio_button to fix this. I have tried this way

<%= f.radio_button :employer, "0" %>
<%= f.radio_button :employer, "1" %>

And I am now trying it this way in my edit.html.erb for users

<div class="field">
  <%= f.label :employer %><br />
  <%= f.label :employer, "Yes", :value => "true"  %>
  <%= f.radio_button :employer, true %>
  <%= f.label :employer, "No", :value => "false" %>
  <%= f.radio_button :employer, false, :checked => true %><br><br>
</div>

Thanks for the future help.

Update: I just looked at my logs and the issue does seem to be with the parameters

 Unpermitted parameters: employer
   (0.1ms)  begin transaction
   (0.0ms)  commit transaction

Trying to fix issue still.

In my application_controller.rb I have

def configure_permitted_parameters
  devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :employer, :state, :city, 
                                                   :bio, :password, :password_confirmation) }
end

def configure_permitted_parameters
  devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:username, :email, :employer, :state, :city, 
                                                   :bio, :avatar, :password, :password_confirmation, :current_password) }
end

for permitting :employer

Validations in user.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable
end
equaltwelve
  • 32
  • 1
  • 6

0 Answers0