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 %>
Related
I have a menu in my header that has a show basket and a login button, each work when the code is placed in separately but not when both lines are in the file.
I'm using devise for the users.
Is there a better way to link to the current basket?
<li><%= link_to basket_path(#basket.id) do %>
<%= image_tag "/assets/viewBasket.png" %>
</li>
<% end %>
<% if signed_in? %>
<li><%= link_to edit_user_registration_path do%>
<%= image_tag"/assets/my_account.png" %></a></li>
<% end %>
<li><%= link_to destroy_user_session_path do%>
<%= image_tag"/assets/logout.png" %></li>
<%end%>
<% else %>
<li><%= link_to new_user_session_path do%>
<%= image_tag"/assets/loginRegisterBtn.png" %></li>
<% end%>
<% end %>
If I run on its own this works but not with the code after.
<li><%= link_to basket_path(#basket.id) do %>
<%= image_tag "/assets/viewBasket.png" %></li>
<% end %>
I think its to do with the way the current basket is set with the session id in the current_basket model.
module CurrentBasket
private
def set_basket
#basket = Basket.find(session[:basket_id])
rescue ActiveRecord::RecordNotFound
#basket = Basket.create
session[:basket_id] = #basket.id
end
end
The closing of <li> must be after the end of the link, like this:
<li>
<%= link_to basket_path(#basket.id) do %>
<%= image_tag "/assets/viewBasket.png" %>
<% end %>
</li>
I used the answer above which helped with one issue, however, I found that I had defined only the shop and index page. Removing this and it now works.
include CurrentBasket
before_action :set_basket, only: [:index, :shop]
Hi i'm new to rails and trying to figure out how to build a search for to get info back from Soundcloud.
If i build everything static then i get back the info but i cannot get it to work with a search form
Method in Controller:
def search
#client = Soundcloud.new(:client_id => ENV["SOUNDCLOUD_CLIENT_ID"])
end
THIS WORKS
#search.html.erb
<div class="form">
<% #client.get('/tracks', :q => 'djsneak' ).each do |track| %>
<ul>
<li><%= track.title %></li>
<li><%= image_tag track.artwork_url %></li>
</ul>
<% end %>
</div>
What i would like to use:
<!--search.html.erb-->
<div class="form">
<%= form_tag("soundcloud_search", method: "get") do %>
<%= label_tag(:q, "Search for:") %>
<%= text_field_tag(:q) %>
<%= submit_tag("Search") %>
<% end %>
</div>
<div>
<h3>results</h3>
<ul>
<li><%= track.title %></li>
<li><%= image_tag track.artwork_url %></li>
</ul>
</div>
i solved it myself, but for who is interested:
<!--search.html.erb-->
<!--search form-->
<div class="form">
<%= form_tag("searchit", method: "get") do %>
<%= label_tag(:q, "Search for:") %>
<%= text_field_tag(:q, params[:q]) %>
<%= submit_tag("Search", :name => nil) %>
<% end %>
</div>
<!--Results-->
<div class="form">
Search Results
<% #client.get('/tracks', :q => params[:q] ).each do |track| %>
<ul>
<li><%= track.title %></li>
<li><%= image_tag track.artwork_url %></li>
</ul>
<% end %>
</div>
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 %>
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 %>
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>