Ruby on rails 如何在ruby中将布尔输出更改为字符串值
我有一个表单,其中我的用户输入一个人作为对婚礼邀请的回复。他们输入姓名、菜单选项,然后选择:出席-是/否-然后,我用标签统计正确和错误的金额,以便用户可以看到有多少人参加或没有参加 我的问题在于桌子本身。在RSVP列所处的位置,我现在得到的是“真”或“假”。在Ruby中,我是否可以将其更改为index.html.erb的字符串值 索引Ruby on rails 如何在ruby中将布尔输出更改为字符串值,ruby-on-rails,ruby,Ruby On Rails,Ruby,我有一个表单,其中我的用户输入一个人作为对婚礼邀请的回复。他们输入姓名、菜单选项,然后选择:出席-是/否-然后,我用标签统计正确和错误的金额,以便用户可以看到有多少人参加或没有参加 我的问题在于桌子本身。在RSVP列所处的位置,我现在得到的是“真”或“假”。在Ruby中,我是否可以将其更改为index.html.erb的字符串值 索引 <% @replies.each do |reply| %> <tr> <td><%= reply.name
<% @replies.each do |reply| %>
<tr>
<td><%= reply.name %></td>
<td><%= reply.menu %></td>
<td><%= reply.rsvp %></td>
<td><%= link_to 'Show', reply, class: "btn" %></td>
<td><%= link_to 'Edit', edit_reply_path(reply), class: "btn" %></td>
<td><%= link_to 'Delete', reply, method: :delete, data: { confirm: 'Are you sure?' }, class: "btn btn-danger" %></td>
<td><%= reply.user.full_name %></td>
</tr>
<% end %>
回复.rb
class Reply < ActiveRecord::Base
attr_accessible :menu, :name, :rsvp, :user_id
belongs_to :user
def self.find_attending
Reply.where(:rsvp => "true").count
end
def self.find_not_attending
Reply.where(:rsvp => "false").count
end
end
<%= f.input :user_id, collection: User.all, label_method: :full_name, :label => 'Added By' %>
<%= f.input :name, :label => 'Person(s) Name' %>
<%= f.input :menu, :label => 'Menu Choice' %>
<%= f.collection_radio_buttons :rsvp, [[true, 'Attending'] ,[false, 'Not Attending']], :first, :last %>
class CreateReplies < ActiveRecord::Migration
def change
create_table :replies do |t|
t.string :name
t.text :menu
t.boolean :rsvp, :default => false
t.timestamps
end
end
end
类回复“true”)。计数
结束
def self.find_not_出席
回复。其中(:rsvp=>“false”)。计数
结束
结束
\u form.html.erb
class Reply < ActiveRecord::Base
attr_accessible :menu, :name, :rsvp, :user_id
belongs_to :user
def self.find_attending
Reply.where(:rsvp => "true").count
end
def self.find_not_attending
Reply.where(:rsvp => "false").count
end
end
<%= f.input :user_id, collection: User.all, label_method: :full_name, :label => 'Added By' %>
<%= f.input :name, :label => 'Person(s) Name' %>
<%= f.input :menu, :label => 'Menu Choice' %>
<%= f.collection_radio_buttons :rsvp, [[true, 'Attending'] ,[false, 'Not Attending']], :first, :last %>
class CreateReplies < ActiveRecord::Migration
def change
create_table :replies do |t|
t.string :name
t.text :menu
t.boolean :rsvp, :default => false
t.timestamps
end
end
end
“%”添加的“”
'个人姓名''%1!'>
'菜单选项'%>
db
class Reply < ActiveRecord::Base
attr_accessible :menu, :name, :rsvp, :user_id
belongs_to :user
def self.find_attending
Reply.where(:rsvp => "true").count
end
def self.find_not_attending
Reply.where(:rsvp => "false").count
end
end
<%= f.input :user_id, collection: User.all, label_method: :full_name, :label => 'Added By' %>
<%= f.input :name, :label => 'Person(s) Name' %>
<%= f.input :menu, :label => 'Menu Choice' %>
<%= f.collection_radio_buttons :rsvp, [[true, 'Attending'] ,[false, 'Not Attending']], :first, :last %>
class CreateReplies < ActiveRecord::Migration
def change
create_table :replies do |t|
t.string :name
t.text :menu
t.boolean :rsvp, :default => false
t.timestamps
end
end
end
class CreateRepliesfalse
t、 时间戳
结束
结束
结束
我对Ruby很陌生,如果您有任何建议,我将不胜感激。非常感谢。只需使用三元:
reply.rsvp ? "true" : "false"
将“true”和“false”替换为您想要显示的任何字符串。关于
“#{reply.rsvp}”
如何?看起来更干净,不是吗?所以我在这里学到了一些东西,你能解释一下吗。它是否只是解释了真/假值,基本上这里我给页面分配了一个打印值,而不是标准的真/假值?这真是一种享受,谢谢你!我不知道你的意思reply.rsvp
计算结果为true
或false
(布尔值)。当你应用三元数时,你说如果值是true
,返回下一个值(这里是字符串“true”
),如果值是false
,则返回:
(这里是字符串“false”
)后面的值。是的,我不太理解,但我现在明白了,谢谢,对于非布尔值,您实际上也可以使用相同的技巧。例如,如果reply.rsvp
的值为nil
,则它将在该上下文中计算为false
,因此三元值将计算为字符串“false”
。不确定这是否有意义,但这是一个很方便的技巧(一般适用于ruby条件句,而不仅仅是三元组)。所以,在这一行中,基本上是这样的:如果reply.rsvp等于“true”,那么返回true;否则,这是一个很好的问题,但它所包含的信息远远超过了提出这个问题所需要的信息。您可能会考虑使用Ruby代码代替实际的Rails代码。这是一个不希望的情况,就是在验证食谱或资源时使用FoeCIST。Foodcritic有时会抱怨这是“不必要的插值”,一些工程师/团队非常严格地要求在涉及任何类型的标准时,您可以忽略什么,也不能忽略什么。虽然我同意你的看法。如果可能的话,我更喜欢这样做。我认为这应该是真正的答案。在处理动态对象时,如果正在计算模板文件,则不希望将“对象”转换为字符串“false”。这将解释这种差异。