Ruby-on-rails 如何在 Rails 1.2.3 中使复选框默认为“已选中”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/586604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I make a check box default to being "checked" in Rails 1.2.3?
提问by daustin777
How can I make a check box default to being "checked" when it is initially displayed?
如何使复选框在最初显示时默认为“已选中”?
I've not found a "Rails" way to do this (that works) so I did it with JavaScript. Is there a proper way to do this in Rails? I'm using Rails 1.2.3.
我还没有找到一种“Rails”方式来做到这一点(有效),所以我用 JavaScript 做到了。在 Rails 中有正确的方法来做到这一点吗?我正在使用 Rails 1.2.3。
回答by Matt Smith
Rails 3.x
导轨 3.x
= form_for(@user) do |f|
= f.check_box :subscribe, {checked: true, ...}
This sets the checked state to true and should work fine. Note the ruby 1.9.x syntax of the hash, for ruby 1.8.x use hash tag format {:checked => true, ...}
这将检查状态设置为 true 并且应该可以正常工作。注意 ruby 1.9.x 的散列语法,对于 ruby 1.8.x 使用散列标签格式 {:checked => true, ...}
回答by wesgarrison
If you're using check_boxin the context of a form, then the check box will show whatever value that field has.
如果您在check_box表单上下文中使用,则复选框将显示该字段具有的任何值。
@user = Person.find(:first)
@user.active = true
check_box 'user', 'active' #=> will be checked by default
If you're using check_box_tag, the third parameter is the initial state (check_box_tagdoc):
如果您正在使用check_box_tag,则第三个参数是初始状态 ( check_box_tagdoc):
check_box_tag "active", 1, true
回答by rebbailey
Simple
简单的
<%= f.check_box :subscribe, checked: "checked" %>
回答by dwhalen
In your controller's new action, try:
在您的控制器的新操作中,尝试:
@user = User.new(:male => true)
Where :male is the attribute you want checked by default on the /users/newpages. This will pass the :maleattribute to the view with a value of true, resulting in a checked box.
其中 :male 是您希望在/users/new页面上默认检查的属性。这会将:male属性传递给值为 的视图true,从而生成一个选中的框。
回答by GEkk
I was shocked that none of the answers solve 100% of the problem.
As all suggested solutions will give checkedresult on edit form, even if the user unchecked the checkbox.
我很震惊,没有一个答案能 100% 解决问题。
由于所有建议的解决方案都会checked在编辑表单上给出结果,即使用户取消选中该复选框。
<%= f.check_box :subscribe, checked: @event.new_record? || f.object.subscribe? %>
<%= f.check_box :subscribe, checked: @event.new_record? || f.object.subscribe? %>
Works for rails 4 +, not tested below.
适用于 rails 4 +,未在下面测试。
回答by phlegx
This works on Rails 2.3.x and Rails 3.0.x!
这适用于 Rails 2.3.x 和 Rails 3.0.x!
On action new in the controller, the check box is set to true.
在控制器中的新操作上,复选框设置为 true。
# in the controller
def new
@user = Person.find(:first)
@user.active = true
end
In the form: the check box is checked on creation (by call new) but if the validation fails the check box remains set with the value that the user has post.
在表单中:该复选框在创建时被选中(通过调用 new),但如果验证失败,该复选框将保持设置为用户已发布的值。
# in the view
<%= form_for ..... |f| %>
...
<%= f.check_box :active %>
...
<% end %>
An other way, but not so good(if you want to change the logic you have to make a new migration) is to set :default => 1in the migration of the given model and attribute.
另一种方法,但不太好(如果你想改变逻辑,你必须进行新的迁移)是在给定模型和属性的迁移中设置:default => 1。
class CreatePeople < ActiveRecord::Migration
def self.up
create_table :people do |t|
...
t.boolean :active, :null => false,
:default => 1
t.timestamps
end
end
def self.down
drop_table :people
end
end
回答by msa.im
I did it in that way.
我就是这样做的。
Add hidden field with value 0 ABOVE check_box_tag
添加值为 0 ABOVE check_box_tag 的隐藏字段
<%= hidden_field_tag :subscribe, '0' %>
<%= check_box_tag :subscribe, '1', params[:subscribe] != '0' %>
On server check with != '0'
在服务器上检查 != '0'
subscribe = params[:subscribe] != '0'

