Rails Bootstrap_form - 单选按钮未在显示页面中呈现标签名称
Rails Bootstrap_form - Radio buttons not rendering label name in the show page
我有一个用 bootstrap_form gem 为 ruby 构建的表单。在表格中我有一些单选按钮。表单在表单的编辑页面中使用正确的标签完美呈现,但是一旦我保存表单并转到 "show" 页面查看结果,单选按钮的输入仅显示单选按钮(这是数字),而不是我分配的自定义标签名称。如何让自定义标签显示而不是显示页面中的值?
这是我的代码:
_form.html.erb:
<%= f.form_group :gender, label: { text: "Gender" } do %>
<%= f.radio_button :gender, 0, label: "Female", checked: true, inline: true %>
<%= f.radio_button :gender, 1, label: "Male", inline: true %>
<% end %>
<%= f.form_group :nationality, label: { text: "Nationality" } do %>
<%= f.radio_button :nationality, 0, label: "Kuwaiti", checked: true, inline: true %>
<%= f.radio_button :nationality, 1, label: "Non-Kuwaiti", inline: true %>
<% end %>
show.html.erb
<p><strong>Full Name:</strong> <%= @profile.name %></p>
<p><strong>Civil ID no:</strong> <%= @profile.civil %></p>
<p><strong>Gender:</strong> <%= @profile.gender %></p>
<p><strong>Nationality:</strong> <%= @profile.nationality %></p>
<p><strong>Mobile no:</strong> <%= @profile.mobile %></p>
<p><strong>Personal Email:</strong> <%= @profile.personal_email %></p>
提前感谢您的帮助!
更新:-
新表格代码:-
<%= f.form_group :gender, label: { text: "Gender" } do %>
<%= f.radio_button :gender, 1 %>
<%= f.label 'Female', inline: true, checked: true %>
<%= f.radio_button :gender, 0 %>
<%= f.label 'Male', inline: true %>
<% end %>
尝试了这个建议,但仍然遇到同样的问题,只是单选按钮不再对齐并且格式不正确。
您需要一个单独的标签标签。而不是:
<%= f.radio_button :gender, 1, label: "Male", inline: true %>
你需要:
<%= f.radio_button :gender, 1 %>
<%= f.label 'Male, inline: true %>
这个答案可能也值得一看:Bootstrap 3: does form-horizontal work for radio buttons with a control-label?
它不使用表单标签助手,但看起来它可以满足您的需要。
您需要查找 table
您已保存与单选按钮对应的数值。因此,当您在 show.htm.erb 中显示变量时,它显示的是从数据库记录中检索到的数值。这是有道理的。但是你需要与数字关联的字符串,这需要查找 table
在表单中创建标签不会为您的字段创建自定义标签。要创建自定义字段标签,您需要为您的 activerecord 模型设置 i18n 本地化,这是一个很好的解决方案,但也需要一些时间和学习曲线。
正在创建基于散列的查找 table。
主动哈希gem
您可以编写自己的查找助手或使用此 gem 来简化基于散列的查找 table 的实现。
例子
# in app/models/person.rb
class Gender < ActiveHash::Base
self.data = [
{:id => 0, :gender => "Female"},
{:id => 1, :gender => "Male"}
]
end
来自他们的规格
We wrote ActiveHash so that we could use simple, in-memory, ActiveRecord-like data structures that play well with Rails forms...
gem 中的构建错误(更新)
我刚刚检查过,他们的构建似乎有错误。可能最好暂时避免。
使用视图助手将布尔值转换为字符串(已添加)
您可以在 /helpers/profile_helper.rb 中实现助手。我称它们为 "lookups," 但这可能不标准。
为每个需要转换为字符串的布尔值创建一个助手
#profile_helper.rb
# Each of your booleans
def gender_to_s(value)
value ? "Male" : "Female"
end
def nationality_to_s(value)
value ? "Kuwaiti" : "Non-Kuwaiti"
end
# Generic true/false conversion
def boolean_to_s(value)
value ? "Yes" : "No"
end
注意:我会一致地命名助手以匹配字段名称,因此在您看来,调用哪个 "to_s" 非常明显。
在您看来
<p><strong>Gender:</strong> <%= gender_to_s(@profile.gender) %></p>
注意:如果你想要一个单一的视图助手,这可以在 application_helper.rb 中实现并包含一个传递的参数 "type" 作为开关控件,但我认为最直接的解决方案是创建每个字段的视图助手。
好的,多一种方法,这样你就有了全套可供选择。在 Rails 4.1 中,枚举数据类型在 ActiveRecord 中实现,如 release notes 的第 2.5 节所述。如果您是 运行 Rails 4.1+,则可以利用此新功能。
本质上,您需要编写迁移以将布尔值和其他数据类型更改为枚举。例如,对于性别示例,与其让性别 0 表示女性,1 表示男性,并为此维护一个映射,您可以简单地使用:
enum gender: [:female, :male]
你的其他属性也是如此。
这最终应该更易于维护,并有助于使您的应用程序代码尽可能透明。
<label><%= form.radio_button :gender, 1, hide_label: true, inline: true%>Female</label>
<label><%= form.radio_button :gender, 0, hide_label: true, inline: true%>Male</label>
对我有用 Bootstrap 4.1
我有一个用 bootstrap_form gem 为 ruby 构建的表单。在表格中我有一些单选按钮。表单在表单的编辑页面中使用正确的标签完美呈现,但是一旦我保存表单并转到 "show" 页面查看结果,单选按钮的输入仅显示单选按钮(这是数字),而不是我分配的自定义标签名称。如何让自定义标签显示而不是显示页面中的值?
这是我的代码:
_form.html.erb:
<%= f.form_group :gender, label: { text: "Gender" } do %>
<%= f.radio_button :gender, 0, label: "Female", checked: true, inline: true %>
<%= f.radio_button :gender, 1, label: "Male", inline: true %>
<% end %>
<%= f.form_group :nationality, label: { text: "Nationality" } do %>
<%= f.radio_button :nationality, 0, label: "Kuwaiti", checked: true, inline: true %>
<%= f.radio_button :nationality, 1, label: "Non-Kuwaiti", inline: true %>
<% end %>
show.html.erb
<p><strong>Full Name:</strong> <%= @profile.name %></p>
<p><strong>Civil ID no:</strong> <%= @profile.civil %></p>
<p><strong>Gender:</strong> <%= @profile.gender %></p>
<p><strong>Nationality:</strong> <%= @profile.nationality %></p>
<p><strong>Mobile no:</strong> <%= @profile.mobile %></p>
<p><strong>Personal Email:</strong> <%= @profile.personal_email %></p>
提前感谢您的帮助!
更新:-
新表格代码:-
<%= f.form_group :gender, label: { text: "Gender" } do %>
<%= f.radio_button :gender, 1 %>
<%= f.label 'Female', inline: true, checked: true %>
<%= f.radio_button :gender, 0 %>
<%= f.label 'Male', inline: true %>
<% end %>
尝试了这个建议,但仍然遇到同样的问题,只是单选按钮不再对齐并且格式不正确。
您需要一个单独的标签标签。而不是:
<%= f.radio_button :gender, 1, label: "Male", inline: true %>
你需要:
<%= f.radio_button :gender, 1 %>
<%= f.label 'Male, inline: true %>
这个答案可能也值得一看:Bootstrap 3: does form-horizontal work for radio buttons with a control-label?
它不使用表单标签助手,但看起来它可以满足您的需要。
您需要查找 table
您已保存与单选按钮对应的数值。因此,当您在 show.htm.erb 中显示变量时,它显示的是从数据库记录中检索到的数值。这是有道理的。但是你需要与数字关联的字符串,这需要查找 table
在表单中创建标签不会为您的字段创建自定义标签。要创建自定义字段标签,您需要为您的 activerecord 模型设置 i18n 本地化,这是一个很好的解决方案,但也需要一些时间和学习曲线。
正在创建基于散列的查找 table。
主动哈希gem
您可以编写自己的查找助手或使用此 gem 来简化基于散列的查找 table 的实现。
例子
# in app/models/person.rb
class Gender < ActiveHash::Base
self.data = [
{:id => 0, :gender => "Female"},
{:id => 1, :gender => "Male"}
]
end
来自他们的规格
We wrote ActiveHash so that we could use simple, in-memory, ActiveRecord-like data structures that play well with Rails forms...
gem 中的构建错误(更新)
我刚刚检查过,他们的构建似乎有错误。可能最好暂时避免。
使用视图助手将布尔值转换为字符串(已添加)
您可以在 /helpers/profile_helper.rb 中实现助手。我称它们为 "lookups," 但这可能不标准。
为每个需要转换为字符串的布尔值创建一个助手
#profile_helper.rb
# Each of your booleans
def gender_to_s(value)
value ? "Male" : "Female"
end
def nationality_to_s(value)
value ? "Kuwaiti" : "Non-Kuwaiti"
end
# Generic true/false conversion
def boolean_to_s(value)
value ? "Yes" : "No"
end
注意:我会一致地命名助手以匹配字段名称,因此在您看来,调用哪个 "to_s" 非常明显。
在您看来
<p><strong>Gender:</strong> <%= gender_to_s(@profile.gender) %></p>
注意:如果你想要一个单一的视图助手,这可以在 application_helper.rb 中实现并包含一个传递的参数 "type" 作为开关控件,但我认为最直接的解决方案是创建每个字段的视图助手。
好的,多一种方法,这样你就有了全套可供选择。在 Rails 4.1 中,枚举数据类型在 ActiveRecord 中实现,如 release notes 的第 2.5 节所述。如果您是 运行 Rails 4.1+,则可以利用此新功能。
本质上,您需要编写迁移以将布尔值和其他数据类型更改为枚举。例如,对于性别示例,与其让性别 0 表示女性,1 表示男性,并为此维护一个映射,您可以简单地使用:
enum gender: [:female, :male]
你的其他属性也是如此。
这最终应该更易于维护,并有助于使您的应用程序代码尽可能透明。
<label><%= form.radio_button :gender, 1, hide_label: true, inline: true%>Female</label>
<label><%= form.radio_button :gender, 0, hide_label: true, inline: true%>Male</label>
对我有用 Bootstrap 4.1