11

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.

1
  • This is a great question, but it's a lot more information than is needed to ask it. You might consider using example Ruby code instead of your actual Rails code. Commented Mar 25, 2016 at 22:02

2 Answers 2

16

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

Sign up to request clarification or add additional context in comments.

2 Comments

One case where this is undesirable is when validating Chef recipes or resources with foodcritic. Foodcritic sometimes complains that this is 'unneccesary interpolation', and some engineers/teams are very strict about what you can and can't ignore when it comes to standards of any sort. Although I agree with you. This is how I prefer to do it when possible.
I think this should be the real answer. When you are dealing with dynamic objects, and you have a template file being evaluated, you don't want an "object" to converted to the string "false". This will account for that difference.
14

Just use a ternary:

reply.rsvp ? "true" : "false"

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

8 Comments

So i learn something here, could you explain this. Does it just account for the true / false values and basically here im assigning a print value to the page instead of the standard true / false? It works a real treat, thank you!
I'm not sure what you mean. reply.rsvp evaluates to either true or false (boolean values). When you apply the ternary, you say if the value is true, return the next value (here, the string "true"), if it is false, return the value after the : (here, the string "false").
Yeh i didn't frase that too well, but i understand now, thanks
You can actually use the same trick for values that are not boolean as well. For example, if the value of reply.rsvp is nil, it will evaluate in this context to false and thus the ternary will evaluate to the string "false". Not sure that makes sense, but it's a handy trick (works for ruby conditionals in general, not just ternaries).
Ternary is a shorthand for if / then / else. So, in this line is essentially saying: if reply.rsvp is equal to "true" then return true; else return false
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.