How to change my boolean output to a string value in ruby - ruby-on-rails

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.

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

Just use a ternary:
reply.rsvp ? "true" : "false"
Replace "true" and "false" by whatever strings you want to display.

Related

Rails show string not ID in index

I have the following form where a user selects a department from a list of database entries on the activity centers page. I am trying to display the department string (:department) instead of the department ID. I tried #activity_center.department.department but that isn't working. Any suggestions?
Form:
<%= f.select :department_id, options_from_collection_for_select(#departments, 'id', 'department'), hide_label: true, :multiple => false %>
Departments Model:
class Department < ActiveRecord::Base
validates :department, :presence => true
has_many :activity_centers
end
Activity Centers Model:
class ActivityCenter < ActiveRecord::Base
validates :activity_center, :presence => true
validates :department_id, :presence => true
belongs_to :departments
end
Activity centers index page:
<% #activity_centers.each do |activity_center| %>
<tr>
<td><%= activity_center.activity_center %></td>
<td><%= activity_center.department.department %></td>
<td style="text-align:right;">
<%= link_to 'View Activity Center', activity_center_path(activity_center), class: "btn btn-success btn-xs" %>
<%= link_to 'Edit', edit_activity_center_path(activity_center), class: "btn btn-default btn-xs" %>
<%= link_to 'Delete', activity_center_path(activity_center), class: "btn btn-danger btn-xs",
method: :delete,
data: { confirm: 'Confirm you want to delete this activity_center.' } %></td>
</tr>
<% end %>
Per MZaragoza it was a typo. belongs_to is supposed to be singular.

Rails 4 How to display belongs_to record in a list

so please bear with me :)
group.rb
class Group < ActiveRecord::Base
has_many :categories, dependent: :destroy
validates :name, :presence => true, :uniqueness => { :case_sensitive => false }
validates :content, :presence => true
end
category.rb
class Category < ActiveRecord::Base
belongs_to :group
validates :name, :presence => true, :uniqueness => { :case_sensitive => false }
validates :content, :presence => true
validates :group_id, :presence => true
end
app/views/categories/index.html.erb
<% #categories.each do |category| %>
<tr>
<td><%= category.id %></td>
<td>
<%= link_to category.name, admin_category_path(category) %>
</td>
<td><%= ***[GROUP WHERE CURRENT CATEGORY BELONGS TO]*** %></td>
<td>
<%= link_to admin_category_path(category), class: "btn btn-info btn-xs" do %>
<i class="fa fa-search"></i>
<% end %>
<%= link_to edit_admin_category_path(category), class: "btn btn-primary btn-xs" do %>
<i class="fa fa-pencil"></i>
<% end %>
<%= link_to admin_category_path(category), method: :delete, data: { confirm: 'Are you sure?' }, class: "btn btn-danger btn-xs" do %>
<i class="fa fa-trash-o"></i>
<% end %>
</td>
</tr>
<% end %>
My question is: how to display a group belongs_to this categories?
It's simple, just write:
<td><%= category.group %></td>
Or
<td><%= category.group.name %></td>
One good idea is to define the function "to_s" for your classes.
For example
group.rb
class Group < ActiveRecord::Base
def to_s
name
end
That way Rails will automatically output the name for the group when printing the group meaning that
<td><%= category.group %></td>
and
<td><%= category.group.name %></td>
Will output the same information.
Use <%= category.group.name %> to display a group belongs_to this categories.

rails nested_forms error and logic

I'am trying to add some fields to my nested form. I've included the gem nested_forms (https://github.com/ryanb/nested_form).
For my prebuilt maps, it works fine, but i can't add new fields.
My controller:
def new
#people = Person.all
#vehicles = Vehicle.all
#roles = Role.all
#pratice_people = []
#people.each do |a|
if a.at1 == true
#pratice_people << a
end
end
#practice = Practice.new
#pratice_people.count.times { #practice.uebung_maps.build }
render action: "new"
end
and my form:
<% #runs = 0 %>
<%= f.fields_for :uebung_maps do |map| %>
<tr>
<%= map.hidden_field :role_id, :id => "role_id_#{#runs}" %>
<%= map.hidden_field :vehicle_id, :id => "vehicle_id_#{#runs}" %>
<%= map.hidden_field :person_id , :value => #pratice_people[#runs].id %><br/>
<td><%= #pratice_people[#runs].name %></td>
<td><%= map.select :role_id, options_from_collection_for_select(#roles, :id, :name), :include_blank => true %></td>
<td><%= map.select :vehicle_id, options_from_collection_for_select(#vehicles, :id, :name), :include_blank => true %></td>
<td><%= map.text_field :time %></td>
</tr>
<% #runs += 1 %>
<% end %>
<%= f.link_to_add "+" , :uebung_maps %>
If i try to access the page, i get following error report
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
Do I have to (or how to) create a logic to rerun Practice.uebung_maps.build?, because I thought this is done within the nested_forms gem....
First, make sure the models are created correctly.
class Practice < ActiveRecord::Base
has_many :uebung_maps
accepts_nested_attributes_for :uebung_maps
end
class UebungMap < ActiveRecord::Base
end
Second, make sure the form_for is nested correctly
<%= nested_form_for #practice do |f| %>
<%= f.fields_for :uebung_maps do |uebung_maps_form| %>
<%= uebung_maps_form.text_field :time %>
<% end %>
<p><%= f.link_to_add "+", :uebung_maps %></p>
<% end %>

Rails: Multi-model form won't write data to models

Ok this is driving me round the bend. I have three models [which are relevant to this quesiton]: Outfit, Outfit_relationship and Answer. Outfit is the parent model and the others are the childs. The Outfit model looks like this:
class Outfit < ActiveRecord::Base
attr_accessible :user_id, :outfit_origin_id, :outfit_parent_id, :outfitrelationship_id #review before going live
attr_accessible :item_id, :image_size_height, :image_size_width, :image_x_coord, :image_y_coord, :zindex, :outfit_id
attr_accessible :description, :question_id, :user_id, :outfit_id
has_many :answers
has_many :outfit_relationships
accepts_nested_attributes_for :outfit_relationships, :allow_destroy => :true
accepts_nested_attributes_for :answers
Note that the 2nd and 3rd attr_accessible are to access the attributes from the other models. I'm not sure this is absolutely necessary, some articles say it is, some say it isn't, so I put it in.
I've created a multi-model form for this data which I want to publish with one button. Here is the code:
<%= form_for(#outfit) do |post_outfit| %>
<%= post_outfit.fields_for #outfit.outfit_relationships do |build| %>
<table>
<tr>
<td>X Coord <%= build.text_area :image_x_coord, :size => '1x1' %></td>
<td>Y Coord <%= build.text_area :image_y_coord, :size => '1x1' %></td>
<td>Z Index <%= build.text_area :zindex, :size => '1x1' %></td>
<td>Height <%= build.text_area :image_size_height, :size => '1x1' %></td>
<td>Weight <%= build.text_area :image_size_width, :size => '1x1' %></td>
</tr>
</table>
<% end %>
<%= post_outfit.fields_for #outfit.answers do |build| %></br></br>
<%= image_tag current_user.fbprofileimage, :size => "40x40" %></br>
<%= current_user.name %></br>
Comment: <%= build.text_area :description, :size => '10x10' %>
<% end %>
<%= post_outfit.fields_for #outfit do |build| %> </br>
origin id: <%= build.text_area :outfit_origin_id, :size => '1x1' %></br>
parent id: <%= build.text_area :outfit_parent_id, :size => '1x1' %></br>
<% end %>
<div id="ss_QID_actions_container">
<%= post_outfit.submit "Submit checked", :class => "btn btn-small btn-primary" %>
</div>
<% end %>
And here are the relevant buts of the outfit controller:
def new
#outfit = Outfit.new
#outfit.save
#outfit.answers.build
#outfit.outfit_relationships.build
respond_to do |format|
format.html # new.html.erb
format.json { render json: #outfit }
end
end
def create
#outfit = Outfit.new(params[:id])
#comment = #outfit.answers.create(params[:answer])
#outfitrelationship = #outfit.outfit_relationships.create(params[:outfit_relationship])
redirect_to outfit_path(#outfit)
So the problem is nothing gets written into my database apart from the IDs. I'm sure I'm dong something stupid here, but can't figure out why.

Ruby: UnknownAttributeError

i have some Orders that can have several Items and these Items have an associated Kind. The Kind can belong to many Items. but i get a "unknown attribute: kinds" in my OrdersController when i hit the submit form button. I use nested forms btw.
Order.rb
class Order < ActiveRecord::Base
validates_presence_of :ref_nr, :total_price
has_many :items, :dependent => :destroy
has_many :kinds, :through => :items
accepts_nested_attributes_for :items
accepts_nested_attributes_for :kinds
validates_associated :items
validates_associated :kinds
end
Item.rb
class Item < ActiveRecord::Base
belongs_to :order
has_one :kind
accepts_nested_attributes_for :kind
validates_associated :kind
end
Kind.rb
class Kind < ActiveRecord::Base
belongs_to :items
end
OrdersController.rb:Create
def create
#order = Order.new(params[:order])
end
new.erb.html
<% form_for #order do |f| %>
<%= f.error_messages %>
<% f.fields_for :items do |builder| %>
<table>
<tr>
<% builder.fields_for :kinds do |m| %>
<td><%= m.collection_select :kind, Kind.find(:all, :order => "created_at DESC"), :id, :name, {:prompt => "Select a Type" }, {:id => "selector", :onchange => "kind_change(this)"} %></td>
<% end %>
<td><%= builder.text_field :amount, :id => "amountField", :onchange => "change_total_price()" %></td>
<td><%= builder.text_field :text, :id => "textField" %></td>
<td><%= builder.text_field :price, :class => "priceField", :onChange => "change_total_price()" %></td>
<td><%= link_to_remove_fields "Remove Item", f %></td>
</tr>
</table>
<% end %>
<p><%= link_to_add_fields "Add Item", f, :items %></p>
<p>
<%= f.label :total_price %><br />
<%= f.text_field :total_price, :class => "priceField", :id => "totalPrice" %>
</p>
<p><%= submit_tag %></p>
<% end %>
i cant see what im missing
You should remove accepts_nested_attributes_for :kinds in Order model and it should be in Item model (as in your code). Then change view here:
...
<% f.fields_for :items do |builder| %>
<table>
<tr>
<% builder.fields_for :kind do |m| %>
...
And I think that you have also mistake in following lines:
<td><%= f.text_field :amount, :id => "amountField", :onchange => "change_total_price()" %></td>
<td><%= f.text_field :text, :id => "textField" %></td>
<td><%= f.text_field :price, :class => "priceField", :onChange => "change_total_price()" %></td>
If fields amount, text and price are associated with Item model, then you should use builder instead of f:
<td><%= builder.text_field :amount, :id => "amountField", :onchange => "change_total_price()" %></td>
<td><%= builder.text_field :text, :id => "textField" %></td>
<td><%= builder.text_field :price, :class => "priceField", :onChange => "change_total_price()" %></td>
EDIT (to answer additional questions from comments):
You should have:
<% f.fields_for :items do |builder| %>
<table>
<tr>
<td><%= builder.collection_select :kind_id, Kind.find(:all, :order => "created_at DESC"), :id, :name, {:prompt => "Select a Type" }, {:id => "selector", :onchange => "kind_change(this)"} %></td>
<td><%= builder.text_field :amount, :id => "amountField", :onchange => "change_total_price()" %></td>
<td><%= builder.text_field :text, :id => "textField" %></td>
<td><%= builder.text_field :price, :class => "priceField", :onChange => "change_total_price()" %></td>
and so on...

Resources