Dear All,
When I use a table from database, if data is not available in table. I will use if and else conditions like
<% if #tables.blank? %>
<p> Not available</p>
<% else %>
blaaa blaa
<% end %>
It works fine. Now I need to apply the same thing for connected tables. If data is available in first table tables and not available in benches. How I can apply??
I tried like this
<% #tables.each do |table| %>
<% if table.benches.blank? %>
<p> Not available</p>
<% else %>
blaaa blaa
<% end %>
<% end %>
... But its not working. Kindly give me suggestions.
I'm not quite sure what you want, but maybe this will help you.
<% if #tables.blank? %>
<p> Not available</p>
<% else %>
<% #tables.each do |table| %>
<% if table.benches.blank? %>
<p> Not available</p>
<% else %>
blaaa blaa
<% end %>
<% end %>
<% end %>
Try using #empty?.
<% #tables.each do |table| %>
<% if table.benches.empty? %>
<p> Not available</p>
<% else %>
blaaa blaa
<% end %>
<% end %>
What happens if you try this:
<% unless table.benches.exists? %>
<p> Not available</p>
<% else %>
blaaa blaa
<% end %>
if/else must output one or the other... make sure you check your HTML source code (not just browser view) to see if the output isn't hiding somewhere inside a tag.
Dear All, Its working... when i apply like this...
<% if #tables.blank? %>
<p> Not available</p>
<% else %>
<% #tables.each do |table| %>
<% table.benches.each do |bench| %>
<% if bench.column.blank? %>
<p> Not available</p>
<% else %>
blaaa blaa
<% end %>
<% end %>
<% end %>
<% end %>
Thank you for everyone who participated in this Question.
Related
I want to display related experiences below the local experiences. I tried to do it using #experiences= Experience.excludes(house.experiences) but didnt work out. Can I get any suggestion how to do that.
<h3>Local experiences</h3>
<section class="list-box">
<% #house.experiences.each do |experience| %>
<p>
<%= experience.name %>
<% if logged_in? && current_user.role == "customer" %>
<%= link_to "Make Enquiry", new_message_path(receiver_id: experience.supplier.user) %>
<% else %>
<%= link_to "Make Enquiry", new_customer_path %>
<% end %>
</p>
<% end %>
</section>
<h3>Related experiences</h3>
<section class="list-box">
<% #experiences.each do |experience| %>
<p>
<%= experience.name %>
<% if logged_in? && current_user.role == "customer" %>
<%= link_to "Make Enquiry", new_message_path(receiver_id: experience.supplier.user) %>
<% else %>
<%= link_to "Make Enquiry", new_customer_path %>
<% end %>
</p>
<% end %>
</section>
There is no method called excludes in the rails model. There is only exclude?, which is a instance method of class String. Refer here.
For your issue, maybe something like below would work given that house_id is the foreign_key in your experience table.
#experiences = Experience.where.not(house_id: house.experiences)
You may refer here for more not conditions.
Beginner alert!
I have this IF statement in my user#show view. I checked if any post for user_id:7 in rails console, it returns the result nil, but when I looked at http://localhost:3000/users/7, I can't see the comment:
You don't have any post yet. Keep posting!
<% #posts.each do |post| %>
<% if #posts != nil %>
<%= link_to "#{post.title}", post_path(post) %>
<% else %>
<p> You don't have any post yet. Keep posting! </p>
<% end %>
<% end %>
Could anyone help me? Thanks!
I think what you need is:
<% if #posts.present? %>
<% #posts.each do |post| %>
<%= link_to "#{post.title}", post_path(post) %>
<% end %>
<% else %>
<p> You don't have any post yet. Keep posting! </p>
<% end %>
If variable #posts is empty, you can't get <p> You don't have any post yet. Keep posting! </p>
User.find(7).posts will return a array. an empty array != nil
Try this:
<% if !#posts.blank? %>
<% #posts.each do |post| %>
<%= link_to "#{post.title}", post_path(post) %>
<% end %>
<% else %>
<p> You don't have any post yet. Keep posting! </p>
<% end %>
How can I simplify the following lines:
<% if #campaign.previous_campaign.present? %>
<%= #campaign.previous_campaign.product_name %>
<% if #campaign.previous_campaign.previous_campaign.present? %>
<%= #campaign.previous_campaign.previous_campaign.product_name %>
<% end %>
<% end %>
I need to keep adding ".previous_campaign" until it is not present. So the next one in the above code would be:
<%= #campaign.previous_campaign.previous_campaign.previous_campaign.product_name %>
etc etc.
Something like this:
<% campaign = #campaign %>
<% while campaign.previous_campaign.present? %>
<% campaign = campaign.previous_campaign %>
<%= campaign.product_name %>
<% end %>
The code may need some debugging, but I guess the idea is clear
You could do something like this:
<% for c in #campaign do %>
<% if c.previous_campaign.present? %>
<%= c.previous_campaign.product_name %>
<% end %>
<% end %>
Hi i need to print out just the candidates where active is == 0 here is my code in the view.
I can print if active is yes or no.. But in the each do loop i just want to print the active candidates.
So how can i add the condition to my each do loop, thanks.
<% #candidates.each do |candidate| %>
<div id="candidateper">
<div class="avatth" ><div class="avat_min">
<% if candidate.avatar.present? %>
<%= link_to (image_tag candidate.avatar.url(:thumb)), (candidate_path(candidate)) %>
<% else %>
<%= link_to (image_tag ("espanol/playersample.png")), (candidate_path(candidate)) %>
<% end %>
</div></div>
<div class="nameth"><%= candidate.name %></div>
<div class="activeth"><%= candidate.active ? t('generales.yess') : t('generales.noo') %></div>
<div class="generalth">
<% if candidate.user.purchased_at.present? %>
<%= candidate.user.purchase_defeated? ? t('generales.defeated') : t('generales.active') %>
<% else %>
<%= t('generales.noo') %>
<% end %>
</div>
<div class="actionsth"><%= link_to t('generales.show'), candidate_path(candidate) %>
<% if current_user.user_type == 'admin' %>
<%= link_to t('generales.delete'), candidate_path(candidate), method: :delete, data: { confirm: t('generales.delete_candidate_confirm') } %>
<% end %>
</div>
</div>
<% end %>
</div>
<% end %>
I`ve tried this
no luck syntax error on all my ideas :P
If candidate.active is actually a boolean then you could say:
<% #candidates.reject(&:active).each do |candidate| %>
...
<% end %>
If #candidates is actually an ActiveRecord::Relation then you could probably say:
<% #candidates.where(:active => false).each do |candidate| %>
...
<% end %>
to avoid pulling a bunch of stuff out of the database when you don't want it.
If active is actually a number (inside the database and outside the database) then you could say:
<% #candidates.select(&:zero?).each do |candidate| %>
...
<% end %>
or
<% #candidates.where(:active => 0).each do |candidate| %>
...
<% end %>
I have a rails app with some views and I'd like to display something different if the view is the array doesn't contain any data. At the moment my view looks like this:
<% #trips.each do |trip| %>
<p><%= trip.id %> | <%= trip.name %></p>
<% end %>
But is there a way I can show some different text if this doesn't have anything inside #trips ?
If you use partials:
trips/_trip.html
<p><%= trip.id %> | <%= trip.name %></p>
your view
<%= render #trips || 'No trip' %>
You can use an if/else conditional check:
<% if (#trips != nil) then %>
<p><%= trip.id %> | <%= trip.name %></p>
<% else %>
<p>Some message</p>
<% end %>
or
<% if (#trips.blank?) then %>
<p>Some message</p>
<% else %>
<p><%= trip.id %> | <%= trip.name %></p>
<% end %>
sure, just use an if there, like this
<% if #trips.blank? %>
<p>No trips</p>
<% else %>
<% #trips.each do |trip| %>
<p><%= trip.id %> | <%= trip.name %></p>
<% end %>
<% end %>
Use a if statement and check if it is null or empty like this
<% if #trips.nil? || #trips.empty? %>
<p>There are no trips</p>
<% else %>
<% #trips.each do |trip| %>
<p><%= trip.id %> | <%= trip.name %></p>
<% end %>
<% end %>
Edit
If working withing rails you would probably want to go with #trips.blank?, if you decide on another ruby framework or project then you can use the above if/else code to check if a array is null or empty