9

我有一个表单,我的用户在其中输入一个人来回复婚礼邀请。他们输入名称、菜单选项,然后选择:参加 - 是/否 - 然后我计算带有标签的真假金额,以便用户可以看到有多少人参加或不参加。

我的问题出在表本身。RSVP 专栏所在的位置,我现在只是得到“真”或“假”。无论如何,在 Ruby 中我可以将其更改为我的 index.html.erb 的字符串值吗?

指数

<% @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

_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 %>

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

我对 Ruby 很陌生,任何指针都将不胜感激。非常感谢。

4

2 回答 2

15

怎么样"#{reply.rsvp}"?看起来更干净,不是吗?

于 2016-04-12T00:57:51.227 回答
14

只需使用三元:

reply.rsvp ? "true" : "false"

用您要显示的任何字符串替换“true”和“false”。

于 2012-11-22T00:08:22.217 回答