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 %>
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]
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 %>
I am trying to refactor some of the code in my view
<% if controller.controller_name == "overview" %>
<div id="left-menu">
<ul>
<li>Office</li>
<hr>
<li><%= link_to "Overview", root_path %></li>
<li><%= link_to "Personnel", personnel_path %></li>
<li><%= link_to "Results", results_path %></li>
<li><%= link_to "Statistics", statistics_path %></li>
</ul>
<ul>
<li>Economy</li>
<hr>
<li>Finances</li>
<li>Contracts</li>
<li>Transfers</li>
<li>Sponsors</li>
</ul>
</div>
<% elsif controller.controller_name == "market" %>
<div id="left-menu">
<ul>
<li>Items</li>
<hr>
<li><%= link_to "Engines", market_engines_path %></li>
<li><%= link_to "Weapons", market_weapons_path %></li>
<li><%= link_to "Armor", market_armor_path %></li>
<li><%= link_to "Chips", market_chips_path %></li>
</ul>
<ul>
<li>Personnel</li>
<hr>
<li><%= link_to "Drivers", market_drivers_path %></li>
<li><%= link_to "Servicemen" %></li>
<li><%= link_to "Programmers" %></li>
<li><%= link_to "Managers" %></li>
</ul>
</div>
<% end %>
Where each <li> corresponds to a method in the controller. I would like to be able to add new methods to my controllers, and then have them dynamically inserted in the view. So is there a way to iterate over the methods in a controller?
As per the answer provided in the referenced question, Controller.action_methods seems to be the method you need, however, to integrate into your code is as follows:
<% lists = %w(items personell) %>
<% for list in lists do %>
<ul>
<li><%= list.titleize %></li>
<hr>
<% items = MarketsController.action_methods %>
<% for item in items do %>
<li><%= link_to item.titleize, eval("market_#{item}_path") %></li>
<% end %>
</ul>
<% end %>
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 %>
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>