2014-10-30 81 views
1

我新的轨道,并试图性别添加到我用迈克尔·哈特尔的教程Rails4 - 如何验证性别[布尔型]字段我的webapp

我决定使用布尔类型性别创建了现有的应用程序。 我添加了新列在迁移文件

DB /迁移/ [时间戳] _add_admin_to_users.rb

class AddAdminToUsers < ActiveRecord::Migration 
    def change 
    add_column :users, :admin, :boolean, default: false 

    #Start - Adding for gender and dob 
    add_column :users, :date_of_birth, :datetime 
    add_column :users, :is_female, :boolean, default: false 
    #End - Adding for gender and dob 

    end 
end 

运行以下与我的新列创建表

$bundle exec rake db:migrate 

决定创建选择框,因为我无法在重复尝试后创建单选按钮

应用程序/模型/ user.rb

class User < ActiveRecord::Base 
. 
. 
GENDER_TYPES = [ ["Male","0"], [ "Female","1" ] ] 
validates :is_female, presence: true, inclusion: { in: ["0","1"] } 
. 
. 

更新测试 测试/模型/ user_test.rb

require 'test_helper' 

class UserTest < ActiveSupport::TestCase 

    def setup 
    @user = User.new(name: "Example User", email: "[email protected]", 
        password: "foobar", password_confirmation: "foobar", 
        date_of_birth: "08/04/1987", is_female: "0") 
    end 
. 
. 

这是我的观点注册表格

的选择框片段

app/views/users/new.html.erb

<%= f.label :is_female, "Gender" %> 
<%= f.select :is_female, User::GENDER_TYPES, class: 'form-control' %> 

这是从HTML注册表格

   <div> 
       <label for="user_is_female">Gender</label> 
       <select id="user_is_female" name="user[is_female]"><option value="0">Male</option> 
<option value="1">Female</option></select> 
       </div> 

添加以下是从质量分配漏洞免疫选择框片断

应用程序/控制器/ users_controller.rb

class UsersController < ApplicationController 
. 
. 
    private 

    def user_params 
     params.require(:user).permit(:name, :email, :password, 
            :password_confirmation, :is_female, :date_of_birth) 
    end 
. 
. 

日志从轨道webrick服务器

在浏览器

--- !ruby/hash:ActionController::Parameters 
utf8: "✓" 
authenticity_token: X1CF1B0ds3fn6QbPE4HhD/AAHo9n6D5+e8oHwgyiU4wSzV3hdUeKCE1Mr3PHJKYx7GPhwmpksYVxS/QmqpwjuQ== 
user: !ruby/hash:ActionController::Parameters 
    name: Kumar 
    email: [email protected] 
    password: testtest 
    password_confirmation: testtest 
    is_female: '1' 
    date_of_birth(1i): '2017' 
    date_of_birth(2i): '10' 
    date_of_birth(3i): '30' 
commit: Create my account 
controller: users 
action: create 

从调试器

Started POST "/users" for 1.39.63.200 at 2014-10-30 12:26:54 +0000 
Processing by UsersController#create as HTML 
    Parameters: {"utf8"=>"_", "authenticity_token"=>"X1CF1B0ds3fn6QbPE4HhD/AAHo9n6D5+e8oHwgyiU4wSzV3hdUeKCE1Mr3PHJKYx7GPhwmpksYVxS/QmqpwjuQ==", "user"=>{"name"=>"Kumar", "email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "is_female"=>"1", "date_of_birth(1i)"=>"2017", "date_of_birth(2i)"=>"10", "date_of_birth(3i)"=>"30"}, "commit"=>"Create my account"} 
    (0.1ms) begin transaction 
    User Exists (0.3ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('[email protected]') LIMIT 1 
    (0.1ms) rollback transaction 
    Rendered shared/_error_messages.html.erb (0.7ms) 
    Rendered users/new.html.erb within layouts/application (8.0ms) 
    Rendered layouts/_shim.html.erb (0.1ms) 
    Rendered layouts/_header.html.erb (0.6ms) 
    Rendered layouts/_footer.html.erb (0.3ms) 
Completed 200 OK in 681ms (Views: 571.3ms | ActiveRecord: 1.5ms) 
1.39.63.200 - - [30/Oct/2014:12:26:55 +0000] "POST /users HTTP/1.1" 200 - 0.7922 

信息我碰到下面当我尝试上申请

The form contains 1 error. 

    Is female is not included in the list 

我不能够解决这个错误。好心帮

- 更新 -

感谢@ user3243476,我改变了我的验证如下

validates :is_female, presence: true, inclusion: { in: [true,false] } 

我能够与性别为女性

Started POST "/users" for 1.39.61.148 at 2014-10-30 14:36:10 +0000 
Processing by UsersController#create as HTML 
    Parameters: {"utf8"=>"_", "authenticity_token"=>"KQVNN5mXph8gEeX+JmOGMLCtBcS2HFK8lh0QtSynHuZkmJUC8c2fYIq0TELyxsEOrM76ibuQ3UecnONRiplu0w==", "user"=>{"name"=>"test", "email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "is_female"=>"1", "date_of_birth(1i)"=>"2013", "date_of_birth(2i)"=>"10", "date_of_birth(3i)"=>"30"}, "commit"=>"Create my account"} 
    (0.1ms) begin transaction 
    User Exists (0.3ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('[email protected]') LIMIT 1 
    SQL (0.8ms) INSERT INTO "users" ("activation_digest", "created_at", "date_of_birth", "email", "is_female", "name", "password_digest", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?) [["activation_digest", "$2a$10$QX6Hx/.bV1BHV955bA8WcuESL3GOBDwGl/o9wnE3EsVHRhwm11csW"], ["created_at", "2014-10-30 14:36:10.201241"], ["date_of_birth", "2013-10-30 00:00:00.000000"], ["email", "[email protected]"], ["is_female", "t"], ["name", "test"], ["password_digest", "$2a$10$15qDvXmU4A3yGK/2lpUBQOazyAVb.3ulaYvPEReeJC76cdFrf5mo."], ["updated_at", "2014-10-30 14:36:10.201241"]] 
    (12.0ms) commit transaction 
    Rendered user_mailer/account_activation.html.erb (1.3ms) 
    Rendered user_mailer/account_activation.text.erb (0.5ms) 

UserMailer#account_activation: processed outbound mail in 314.1ms 

创建配置文件,但我无法创建性别为男性的个人资料 我收到新的错误

The form contains 1 error. 

    Is female can't be blank 

这是使用WEBrick登录

Started POST "/users" for 1.39.60.97 at 2014-10-30 15:45:05 +0000 
Processing by UsersController#create as HTML 
    Parameters: {"utf8"=>"_", "authenticity_token"=>"IAlJi57XunoNgnaEiTfZ7kIbMs9k4de2dBSzrNXU5fRtlJG+9o2DBacn3zhdkp7QXnjNgmltWE1+lUBIc+qVwQ==", "user"=>{"name"=>"newmale", "email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "is_female"=>"0", "date_of_birth(1i)"=>"2017", "date_of_birth(2i)"=>"10", "date_of_birth(3i)"=>"30"}, "commit"=>"Create my account"} 
    (0.1ms) begin transaction 
    User Exists (0.3ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('[email protected]') LIMIT 1 
    (0.1ms) rollback transaction 
    Rendered shared/_error_messages.html.erb (5.9ms) 
    Rendered users/new.html.erb within layouts/application (12.8ms) 
    Rendered layouts/_shim.html.erb (0.0ms) 
    Rendered layouts/_header.html.erb (0.5ms) 
    Rendered layouts/_footer.html.erb (0.2ms) 
Completed 200 OK in 1825ms (Views: 1584.9ms | ActiveRecord: 0.6ms) 
1.39.60.97 - - [30/Oct/2014:15:45:07 +0000] "POST /users HTTP/1.1" 200 - 1.9334 

调试日志

--- !ruby/hash:ActionController::Parameters 
utf8: "✓" 
authenticity_token: IAlJi57XunoNgnaEiTfZ7kIbMs9k4de2dBSzrNXU5fRtlJG+9o2DBacn3zhdkp7QXnjNgmltWE1+lUBIc+qVwQ== 
user: !ruby/hash:ActionController::Parameters 
    name: newmale 
    email: [email protected] 
    password: testtest 
    password_confirmation: testtest 
    is_female: '0' 
    date_of_birth(1i): '2017' 
    date_of_birth(2i): '10' 
    date_of_birth(3i): '30' 
commit: Create my account 
controller: users 
action: create 

在轨控制台我尝试以下

无法保存男性用户

>> newmaleuser = User.new(name: "Example male User", email: "[email protected]", password: "foobar", password_confirmation: "foobar", date_of_birth: "08/04/1987", is_female: "0") 
=> #<User id: nil, name: "Example male User", email: "[email protected]", created_at: nil, updated_at: nil, password_digest: "$2a$10$m8NsGUgNZ1fLqzba1JpWLuIlkM47ZpxMtUouMqMpIZP...", remember_digest: nil, admin: false, date_of_birth: "1987-04-08 00:00:00", is_female: false, activation_digest: nil, activated: false, activated_at: nil, reset_digest: nil, reset_sent_at: nil> 
>> newmaleuser.save 
    (41.6ms) begin transaction 
    User Exists (104.7ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('[email protected]') LIMIT 1 
    (0.5ms) rollback transaction 
=> false 

能给... AVE女性用户

>> newfemaleuser = User.new(name: "Example female User", email: "[email protected]", password: "foobar", password_confirmation: "foobar", date_of_birth: "08/04/1987", is_female: "1") 
=> #<User id: nil, name: "Example female User", email: "[email protected]", created_at: nil, updated_at: nil, password_digest: "$2a$10$NDDXC8n5iYAGEWD8VM8efOvM8UdGlj4iwIE/3Knd0vY...", remember_digest: nil, admin: false, date_of_birth: "1987-04-08 00:00:00", is_female: true, activation_digest: nil, activated: false, activated_at: nil, reset_digest: nil, reset_sent_at: nil> 
>> newfemaleuser.save 
    (0.3ms) begin transaction 
    User Exists (0.4ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('[email protected]') LIMIT 1 
    SQL (71.2ms) INSERT INTO "users" ("activation_digest", "created_at", "date_of_birth", "email", "is_female", "name", "password_digest", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?) [["activation_digest", "$2a$10$T1dkwRJET0HPuyfu1XMFrearJSoo9c/2fC7UlZZMdgTWQkDUxTb2y"], ["created_at", "2014-10-30 15:58:17.963568"], ["date_of_birth", "1987-04-08 00:00:00.000000"], ["email", "[email protected]"], ["is_female", "t"], ["name", "Example female User"], ["password_digest", "$2a$10$NDDXC8n5iYAGEWD8VM8efOvM8UdGlj4iwIE/3Knd0vY1oYa.bChWK"], ["updated_at", "2014-10-30 15:58:17.963568"]] 
    (107.1ms) commit transaction 
=> true 
>> 

is_female: “1”is_female: “真”工作
我不知道为什么
is_female: “0”和is_female: “假”不起作用?

请帮忙。

回答

0

我发现了它。 存在:在

validates :is_female, presence: true, inclusion: { in: [true,false] } 

是造成这个问题。 实测值中下面的 “钢轨4路由O.费尔南德斯,K.福斯蒂诺& V.库什纳”

validates_inclusion_of :is_female, in: [true, false] 

这是工作。 Yippie和所有的测试都通过了。

1

由于性别是一个布尔字段,你应该使用

validates :is_female, presence: true, inclusion: { in: [true,false] } 
+0

谢谢我能够创建性别为女性的配置文件,但我得到了与男性创建的配置文件相同的错误。 – 2014-10-30 14:37:20

+0

你的测试也失败了? – 2014-10-30 14:46:53

+0

是的测试失败。我用新的电子邮件更新了失败的日志@ user3243476 – 2014-10-30 15:07:36