I have a serialized hash of a checkbox, and i would like to display just the selected keys.
Model
class Policy < ActiveRecord::Base
belongs_to :user
serialize :shipping, JSON
end
View
<ul>
<%= p.fields_for :shipping, #policy.shipping do |s|%>
<li><%= s.check_box :fedex %><%= s.label :fedex %></li>
<li><%= s.check_box :dhl %><%= s.label :dhl %></li>
<li><%= s.check_box :usps_10 %><%= s.label :usps %></li>
</ul>
<% end %>
BD
{"fedex":"1","usps":"0","dhl":"0"}
View Show
<ul>
<% if !#policy.shipping.nil? %>
<% #policy.shipping.keys.each do |key|%>
<li><%= key %></li>
<% end %>
<% end %>
</ul>
Your #policy.shipping should be a plain old Hash so select should sort you out:
<ul>
<% if #policy.shipping.present? %>
<% #policy.shipping.select { |k, v| v == 1 }.keys.each do |key| %>
<li><%= key %></li>
<% end %>
<% end %>
</ul>
I'm not sure off the top of my head if you'll get 1 or '1' in your values though so you might need to v == '1' inside the select block.
You can skip the .present? (or !...nil?) check by taking advantage of the fact that nil.to_a is [] as well:
<ul>
<% #policy.shipping.to_a.select { |k, v| v == 1 }.map(&:first).each do |key| %>
<li><%= key %></li>
<% end %>
</ul>
Or even:
<ul>
<% #policy.shipping.to_a.select { |k, v| v == 1 }.each do |a| %>
<li><%= a.first %></li>
<% end %>
</ul>
And you probably want to fix your original view to be properly nested or you might run into strange things in the future:
<ul>
<%= p.fields_for :shipping, #policy.shipping do |s|%>
<li><%= s.check_box :fedex %><%= s.label :fedex %></li>
<li><%= s.check_box :dhl %><%= s.label :dhl %></li>
<li><%= s.check_box :usps_10 %><%= s.label :usps %></li>
<% end %>
</ul>
Related
I need to print relations Emissions With trades.
Example:
-Trade 1
Emission 1
Emission 2
Actuallly my code print.
-Trade 1
Emission 1
-Trade 1
Emission 2
This code:
<% #emissions.group_by(&:trade).each do |trade, emission| %>
<% emission.each do |e| %>
<% if (e.users.present?) %>
<li><%= trade.name %></li>
<ul>
<li><%= e.name %></li>
</ul>
<% end %>
<% end %>
</li>
<% end %>
UPDATE
The list should appear if the user relationship is associated with the emission.
For instance:
<% If (e.users.present?)%>
<% = emission.name%>
This is only displayed if the emission ratio with the user exists.
The company name should appear if this relationship exists.
Try the following
<% If (emission.users.present?)%>
<% = trade.name%>
<% = emission.name%>
But this does nothing but repeat twice for each issue the company name.
Your indentation is all over the place and you have lots of html tags which have an <% end %> in the middle, and stuff like that, so it's a bit confusing, but i think that you're trying to do something like this:
<% #emissions.group_by(&:trade).each do |trade, grouped_emissions| %>
<li><%= trade.name %>
<ul>
<% grouped_emissions.each do |emission| %>
<% if (emission.users.present?) %>
<li><%= e.name %></li>
<% end %>
<% end %>
</ul>
</li>
<% end %>
How to remove the end of the "s" in partial "_error_messages".
<% if event.errors.any? %>
<div id="errorExplanation">
<h2> В форме обнаружено <%= pluralize(event.errors.count, "ошибки") %>:</h2>
<ul>
<% event.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
You need to do...
pluralize(event.errors.count, "ошибка", "ошибки")
It will use the singular term if one, and the plural term otherwise.
Below is the code which gives me list of accounts.
<% #accounts.each do |account| %>
<li><%= link_to account.name %></li>
<% end %>
How to access the list of projects of my specific account.
Try this:
<% #accounts.each do |account| %>
<li><%= link_to account.name %></li>
<li><%= link_to account.projects %></li> #this will give you a collection of projects associated with that account
<% end %>
If you want a link_to for each individual project then you'll have to use another loop like this:
<% #accounts.each do |account| %>
<li><%= link_to account.name %></li>
<% account.projects.each do |project| %>
<li><%= link_to project %></li> #this will give you individual project associated with that account
<% end %>
<% end %>
Edit:
Incase you don't have any projects for an account you can do:
<% #accounts.each do |account| %>
<li><%= link_to account.name %></li>
<% if account.projects %>
<% account.projects.each do |project| %>
<li><%= link_to project %></li> #this will give you individual project associated with that account
<% end %>
<% else %> # add this else block to execute your code when there are on projects
<p> No projects associated with your account</p>
<% end %>
<% end %>
Firstly, I am hoping my understanding is correct and I do in fact have a an array within my hash.
I've gotten so far but cant seem to iterate through each hash and its array. What I would like to do is have each tournament grouped by date (year only) and have all its tournaments listed underneath.
So far my logic is
Controller
#tournaments = Tournament.all
#tournament_year = #tournaments.group_by {|y| y.tourn_date.year}
View
<ul class="resp-tabs-list">
<% #tournament_year.each do |y, t| %>
<li><%= y %></li>
<% end %>
</ul>
hash
{2014=>[#<Tournament id: 3, name: "WTS ", tourn_date: "2014-04-26", tourn_location: "Cardiff", created_at: "2014-04-26 14:57:21", updated_at: "2014-04-26 14:57:21">, #<Tournament id: 4, name: "CTS Nottingham", tourn_date: "2014-05-26", tourn_location: "Nottingham", created_at: "2014-04-26 14:57:39", updated_at: "2014-04-26 14:57:48">]}
My desired output would be below
<h3>2014</h3>
<ul>
<li>Tournament Name 1</li>
<li>Tournament Name 2</li>
</ul>
<h3>2013</h3>
<ul>
<li>Tournament Name 1</li>
<li>Tournament Name 2</li>
</ul>
Change your view as below:
<ul class="resp-tabs-list">
<% #tournament_year.each do |y, t| %>
<h3><%= y %></h3>
<ul>
<% t.each_with_index do |tournament, i| %>
<li><%= tournament.name %> <%= i %></li>
<% end %>
</ul>
<% end %>
</ul>
I would suggest
<% #tournament_year.each do |y, t| %>
<h3> <%= y %></h3>
<ul class="resp-tabs-list">
<% t.each do |tournament| %>
<li><%= "#{tournament.to_s}" %></li>
<% end %>
</ul>
<% end %>
where
#app/models/tournament.rb
def to_s
"#{name} #{id}"
end
Ive just come up with this solution as an example, seems to work, can anyone see any reason why this would be incorrect?
<ul class="resp-tabs-list">
<%= #tournament_year.each do |y, t| %>
<li><%= y %></li>
<%= #tournament_year[y].each do |x| %>
<li><%= x.name %></li>
<% end %>
<% end %>
</ul>
I'm not sure the best way to iterate through all the columns within my Day model to benefit a simplified formatting scheme.
index.html.erb
<% #day.each do |u| %>
<li id="date"><%= clean_date(u.date) %></li>
<li><b>Morning</b>: <%= u.morning %></li>
<% unless u.morning_notes.blank? %><li><b>Morning Notes</b>: <%= u.morning_notes %><% end %></li>
<li><b>Afternoon</b>: <%= u.afternoon %></li>
<% unless u.afternoon_notes.blank? %><li><b>Afternoon Notes</b>: <%= u.afternoon_notes %><% end %></li>
<li><b>Evening</b>: <%= u.evening %></li>
<% unless u.evening_notes.blank? %><li><b>Evening Notes</b>: <%= u.evening_notes %><% end %></li>
<li><b>Night</b>: <%= u.night %></li>
<% unless u.night_notes.blank? %><li><b>Night Notes</b>: <%= u.night_notes %><% end %></li>
<% end %>
Ideally, it'd be something like:
<% #day.each do |u| %>
<li id="date"><%= clean_date(u.date) %></li>
<li><b>TimeOfDay</b>: <%= u.TimeOfDay %></li>
<% unless u.TimeOfDay_notes.blank? %>
<li><b>TimeOfDay Notes</b>: <%= u.TimeOfDay_notes %>
<% end %></li>
<% end %>
Where TimeOfDay iterates through Morning, Afternoon, Evening and Night.
Maybe something like this:
<% #day.each do |u| %>
<li id="date"><%= clean_date(u.date) %></li>
<% ["morning", "afternoon", "evening", "night"].each do |t| %>
<li><b><%= t.capitalize %></b>: <%= u.send(t) %></li>
<% notes = u.send("#{t}_notes") %>
<% unless notes.blank? %>
<li><b><%= t.capitalize %> Notes</b>: <%= notes %></li>
<% end %>
<% end %>