I have a form where my user enters a person in reply to a wedding invite. They enter a name, menu choice, and select: attending - Yes / No - I then have a count on the true and false amounts with labels so the user can see how many people are attending or not attending.
我有一个表格,我的用户输入一个人回复婚礼邀请。他们输入一个名称,菜单选项,然后选择:出席 - 是/否 - 然后我会用标签计算真假数量,这样用户就可以看到有多少人参加或不参加。
My problem is in the table itself. Where the RSVP column sits, i have at moment just got 'true' or 'false'. Is there anyway in Ruby i can change this to be a string value for my index.html.erb?
我的问题出在桌子本身。在RSVP列所在的位置,我现在得到'真'或'假'。无论如何在Ruby中我可以将其更改为index.html.erb的字符串值吗?
Index
指数
<% @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 %>
reply.rb
reply.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
_form.html.erb
_form.html.erb
<%= 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 %>
db
D b
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
I'm very new to Ruby, any pointers would be appreciated. Many thanks.
我是Ruby的新手,任何指针都会受到赞赏。非常感谢。
2 个解决方案
#1
7
Just use a ternary:
只需使用三元:
reply.rsvp ? "true" : "false"
Replace "true" and "false" by whatever strings you want to display.
用你想要显示的任何字符串替换“true”和“false”。
#2
9
How about "#{reply.rsvp}"
? Seems to be cleaner, doesn't it?
“#{reply.rsvp}”怎么样?似乎更干净,不是吗?
#1
7
Just use a ternary:
只需使用三元:
reply.rsvp ? "true" : "false"
Replace "true" and "false" by whatever strings you want to display.
用你想要显示的任何字符串替换“true”和“false”。
#2
9
How about "#{reply.rsvp}"
? Seems to be cleaner, doesn't it?
“#{reply.rsvp}”怎么样?似乎更干净,不是吗?