validates_acceptance_of 工作但未在数据库中保存 true user:age_valid
validates_acceptance_of working but not saving true user:age_valid in database
validate_acceptance_of 正在运行,但如果选中,它不会将真实值保存到数据库用户列 age_valid。
users.controller.rb
class UsersController < ApplicationController
.
.
.
private
def user_params
params.require(:user).permit(:name, :birthdate, :email, :password,
:password_confirmation, :age_valid)
end
end
_form.html.erb
<%= simple_form_for(@user) do |f| %>
.
.
.
<%= f.input :age_valid,
:as => :boolean,
:label => false,
:inline_label => 'I am 18 years of age or older.' %>
.
.
.
<% end %>
user.rb
class User < ActiveRecord::Base
attr_accessor :remember_token, :age_valid
.
.
.
validates_acceptance_of :age_valid,
:acceptance => true,
:message => "You must verify that you are at least 18 years of age."
这一切都有效接受它不会将数据库列 "age_valid" 从 false 更改为 true。我需要这样做来保存记录。
这是翻译后的DOM
<div class="form-group boolean optional user_age_valid">
<div class="checkbox">
<input value="0" type="hidden" name="user[age_valid]">
<label><input class="boolean optional" type="checkbox" value="1" name="user[age_valid]" id="user_age_valid"> I am 18 years of age or older.</label>
</div>
</div>
使用迁移
class AddAgeValidToUser < ActiveRecord::Migration
def change
add_column :users, :age_valid, :boolean, default: false
end
end
以下适合我。
user.rb
class User < ActiveRecord::Base
validates_acceptance_of :age_valid,
:accept => true,
:message => "You must verify that you are at least 18 years of age."
end
new.html.erb
<%= simple_form_for @user, url: {action: "create"} do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.input :age_valid,
:as => :boolean,
:label => false,
:checked_value => true,
:unchecked_value => false,
:inline_label => 'I am 18 years of age or older.' %>
<%= f.submit %>
我删除了属性访问器并将 :acceptance => true,
更改为 :accept => true
并添加了 :checked_value => true,:unchecked_value => false
您不小心混合了两种定义验证的句法方式。
新语法:
validates :field,
property: {setting: "value"}
旧语法:
validates_property_of :field, setting: "value"
捷径 -- 从参数中删除 acceptance
选项,因为它已在方法名称中说明。
如果您更喜欢新的语法,它看起来像这样:
validates :age_valid, acceptance: {message: "..."}
在这种特定情况下,使用新语法没有任何好处。但是,在其他情况下,它允许您使用多个条件验证属性,而无需重复它们或求助于元编程。
在代码可维护性方面,保持一致:选择其中之一并在项目中需要的任何地方使用它。
validate_acceptance_of 正在运行,但如果选中,它不会将真实值保存到数据库用户列 age_valid。
users.controller.rb
class UsersController < ApplicationController
.
.
.
private
def user_params
params.require(:user).permit(:name, :birthdate, :email, :password,
:password_confirmation, :age_valid)
end
end
_form.html.erb
<%= simple_form_for(@user) do |f| %>
.
.
.
<%= f.input :age_valid,
:as => :boolean,
:label => false,
:inline_label => 'I am 18 years of age or older.' %>
.
.
.
<% end %>
user.rb
class User < ActiveRecord::Base
attr_accessor :remember_token, :age_valid
.
.
.
validates_acceptance_of :age_valid,
:acceptance => true,
:message => "You must verify that you are at least 18 years of age."
这一切都有效接受它不会将数据库列 "age_valid" 从 false 更改为 true。我需要这样做来保存记录。
这是翻译后的DOM
<div class="form-group boolean optional user_age_valid">
<div class="checkbox">
<input value="0" type="hidden" name="user[age_valid]">
<label><input class="boolean optional" type="checkbox" value="1" name="user[age_valid]" id="user_age_valid"> I am 18 years of age or older.</label>
</div>
</div>
使用迁移
class AddAgeValidToUser < ActiveRecord::Migration
def change
add_column :users, :age_valid, :boolean, default: false
end
end
以下适合我。
user.rb
class User < ActiveRecord::Base
validates_acceptance_of :age_valid,
:accept => true,
:message => "You must verify that you are at least 18 years of age."
end
new.html.erb
<%= simple_form_for @user, url: {action: "create"} do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.input :age_valid,
:as => :boolean,
:label => false,
:checked_value => true,
:unchecked_value => false,
:inline_label => 'I am 18 years of age or older.' %>
<%= f.submit %>
我删除了属性访问器并将 :acceptance => true,
更改为 :accept => true
并添加了 :checked_value => true,:unchecked_value => false
您不小心混合了两种定义验证的句法方式。
新语法:
validates :field,
property: {setting: "value"}
旧语法:
validates_property_of :field, setting: "value"
捷径 -- 从参数中删除 acceptance
选项,因为它已在方法名称中说明。
如果您更喜欢新的语法,它看起来像这样:
validates :age_valid, acceptance: {message: "..."}
在这种特定情况下,使用新语法没有任何好处。但是,在其他情况下,它允许您使用多个条件验证属性,而无需重复它们或求助于元编程。
在代码可维护性方面,保持一致:选择其中之一并在项目中需要的任何地方使用它。