Using Loop Variable to Set the id Attribute - ruby-on-rails

I have the following code:
<% #electives.each do |elective| %>
<div>
</div>
<% end %>
I would like to set the id of the div to elective.name, but I don't know how to do it, or whether this works:
<% #electives.each do |elective| %>
<div id="elective.name">
</div>
<% end %>
Is it possible to do this in Rails?
Thanks.

You can use <%= ruby variable %> block within erb file to add ruby values within your pages
<div id="<%= elective.name %>">

As an alternative solution, you can use the rails tag helper
<% #electives.each do |elective| %>
<%= tag.div id: elective.name do %>
...
<% end %>
<% end %>

Related

Rails: how to create a list with conditional links

I am working on a RAILS application where I create a view which lists all available resources of a given model species.rb.
The view partial is:
<% i= #s
for species in #species %>
<%= species.name %>, <%= species.author.surname %> <%= species.author.initial_name %>
<% i -= 1
end %>
Some of the resources species have an related article, others have only the name. I would like to loop through the partial and add a link only to the entries which have an associated article.
Something like: add link if species.article is present else just put species.name without link + loop through it for all entries.
How can I do this?
UPDATE:
Thanks to #jvillian and #fool-dev I was able to progress. In my case I wanted to add a link if the resource has a description in a description row of its table.
<% #species.each do |species| %>
<div class="entry">
<p><i><%= link_to_if(species.txtlandscape.present?, "#{species.name}, #{species.author.surname}, #{species.author.initial_name}. 2014", :controller => 'projects', :action => 'show', :id => species) %></i></p>
</div>
<% end %>
Now that a link is added I was wondering if it can be used to load a partial to a target such as in, where ArticleRequest is a JS function I have:
<% # species.each do | species | %>
<div id="species-<%= species.id %>" class="species-entry">
<a onClick="ArticleRequest('/species/show/<%= species.id %>', 'species-<%= species.id %>');">
<p><%= species.name %></p>
</a>
</div>
<% end %>
Until I find a way to do this with link_to_if, I will use something like:
<% for species in #species %>
<% if species.txtlandscape.present? %>
<a onClick="ArticleRequest('/species/show/<%= species.id %>', 'species-<%= species.id %>');">
<p><%= species.name %>, <%= species.author.surname %> <%= species.author.initial_name %></p>
</a>
<% else %>
<p><%= species.name %>, <%= species.author.surname %> <%= species.author.initial_name %></p>
<% end %>
<% end %>
According to the docs, it seems you should be able to do:
<% #species.each do |specie| %>
<%= link_to_if(specie.article, specie.name, specie_article_path(specie.article)) %>
<% end %>
I made the path name up, you'll have to make that match your actual routes.
BTW, this:
for species in #species
Is super non-idiomatic.
You can do this, see the below
<% for species in #species %>
<% if species.article.present? %> #=> I thin it will be articles because table name is articles, anyway, you know better
<%= link_to species.name, link_path(species.article) %>, #=> on the link_path it will be your proper link just replace this
<% else %>
<%= species.name %>,
<% end %>
<%= species.author.surname %> <%= species.author.initial_name %>
<% end %>
You can do this with Rails each method like below
<% #species.each do |species| %>
<% if species.article.present? %> #=> I thin it will be articles because table name is articles, anyway, you know better
<%= link_to species.name, link_path(species.article) %>, #=> on the link_path it will be your proper link just replace this
<% else %>
<%= species.name %>,
<% end %>
<%= species.author.surname %> <%= species.author.initial_name %>
<% end %>
Or you can use link_to_if it is also most easier to understand
<% #species.each do |species| %>
<%= link_to_if(species.article.present?, "#{species.name},", link_path(species.article)) %>
<%= species.author.surname %> <%= species.author.initial_name %>
<% end %>
Hope it will help.

link_to tag not including all divs

I currently have a link to tag which should wrap around all the content within it, but currently it's not doing that. It's wrapping around the code until it hits another div with a rails query inside it?
index.html.erb
<% #posts.each do |post| %>
<div class="widget" >
<%= link_to post do %>
<div class="image b-lazy" data-src="<%= post.image %>">
</div>
<div class="caption">
<h4><%= post.title %></h4>
<p>by <%= post.affiliate %></p>
</div>
<!-- LINK TO TAG ENDS HERE FOR SOME REASON -->
<div class="caption-top">
<% post.categories.each do |category| %>
<%= link_to category_path(category) do %>
<div class="tag <%= category.name %>"><%= category.name %></div>
<% end %>
<% end %>
</div>
<% end %>
</div>
Any help is appreciated!
Jonathan
Two things:
You are using link_to inside another call to link_to. That is probably not what you want.
The result of a block will be what you return from a block, normally the last line. Take a look at this question for a solution.

How to pass the path for link_to tag from controller to view.

Now I'm trying to make path markup.
Assume it's up to 4th depth level directory then I made something like this for view?
How can I pass the path to view from controller with variable label???
<% if !#FirstDirPath.nil? %>
<%= #FirstDirPath %> <span class="divider">/</span>
<% end %>
<% if !#SecondDirPath.nil? %>
<%= #SecondDirPath %> <span class="divider">/</span>
<% end %>
<% if !#ThirdDirPath.nil? %>
<%= #ThirdDirPath %> <span class="divider">/</span>
<% end %>
<% if !#FourthDirPath.nil? %>
<%= #FourthDirPath %> <span class="divider">/</span>
<% end %>
You can access the current path with request.fullpath
If you want to get the breadcrumb then you can just split the fullpath by '/'. In the controller:
#path = request.fullpath
#breadcrumb = #path.split('/')
In the view:
<% #breadcrumb.each do |crumb| %>
<%= crumb %>
<span class="divider">/</span>
<% end %>
Untested, but I think it should work.

Best practice method of displaying flash messages

I'm wondering what's the best practice for displaying flash messages. The two main ways I've seen are using something like this scaffold generated code
<p id="notice"><%= notice %></p>
or placing code like this in your application header.
<% if !flash.empty? %>
<div id="flash">
<% flash.keys.each do |k| %>
<div class="<%= k %>">
<%= flash[k] %>
</div>
<% end %>
</div>
<% end %>
It appears to me that the first method adds more flexibility while the latter improves code readability and eliminates redundancy. Is there a method most rails developers prefer? As a side question how does scaffolding implement notice? Is it just a helper that accesses the flash hash? Why go through the trouble of using the helper when you can directly use the flash hash? Thanks
I'm doing it this way:
<% flash.each do |key, value| %>
<%= content_tag :div, value, class: "flash #{key}" %>
<% end %>
Calling a partial keeps your application.html.erb even cleaner..
<%= render 'shared/flash_messages' if !flash.empty? %>
.. and in the partial do something like what #zolter mentioned:
<div id="flash_messages">
<% flash.each do |key, value| %>
<%= content_tag(:div, value, :class => "flash #{key}") %>
<% end %>
</div>
Why not put the second method on a helper function so it doesn't affect code readability on layouts?
<% if flash[:notice] %>
<div class="notification is-primary global-notification">
<p class="notice"><%= notice %></p>
</div>
<% end %>
<% if flash[:alert] %>
<div class="notification is-danger global-notification">
<p class="alert"><%= alert %></p>
</div>
<% end %>

Rails: How to name and create unique divs within a loop?

I have a view with a div that is looped many times. Each of the created divs need to have a unique ID so I can access them specifically (at the moment, all my divs have the same ID specified in html so whenever I try to access a specific div it just finds the first one).
This is the version that I currently have (multiple 'rowBox'es are not discernible).
<% #customers.each do |customer| %>
<div id="customer" class="rowBox">
...
</div>
<% end %>
I would like to be able to do something like:
<% #customers.each do |customer| %>
<div id="box<%=customer.id%>">
...
</div>
<% end %>
This doesn't seem to work. Any ideas on how to accomplish this?
Rails has some handy helpers for exactly this.
<% #customers.each do |customer| %>
<%= div_for customer, :class => "rowBox" do %>
...
...
<% end %>
<% end %>
This will produce e.g.:
<div id="customer_1" class="customer rowBox">
...
</div>
<div id="customer_2" class="customer rowBox">
...
</div>
......
<% #customers.each do |customer| %>
<div id=<%= "box#{customer.id}" -%>>
...
</div>
<% end %>
Sorry for earlier omission. This should work.

Resources