Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change my boolean output to a string value in ruby

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?

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

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

db

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.

like image 432
Doidgey Avatar asked Nov 22 '12 00:11

Doidgey


2 Answers

How about "#{reply.rsvp}"? Seems to be cleaner, doesn't it?

like image 57
Jiasen Xu Avatar answered Nov 15 '22 13:11

Jiasen Xu


Just use a ternary:

reply.rsvp ? "true" : "false"

Replace "true" and "false" by whatever strings you want to display.

like image 31
Chris Salzberg Avatar answered Nov 15 '22 15:11

Chris Salzberg