无法使用设计更改用户雇主的布尔值

时间:2014-05-12 16:31:11

标签: ruby-on-rails-4 devise radio-button

当新用户访问我们的网站时,他们可以注册为普通用户或雇主。我有一个布尔字段雇主,当新用户注册时,可以检查其是真还是假。我遇到的问题是我似乎无法保存(在注册时)或通过表单将默认值从false更改为true。我在询问之前以及何时使用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 

我可以将雇主布尔值从默认的false更改为true。我很确定这只是表单的一个问题。有关如何正确实现radio_button来解决这个问题的猜测。我试过这种方式

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

我现在正在我的 edit.html.erb 中为用户尝试这种方式

<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>

感谢您将来的帮助。

更新:我只看了我的日志,问题似乎与参数

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

尝试解决问题。

在我的 application_controller.rb 中,我有

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

允许:雇主

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

0 个答案:

没有答案