Im trying to restrict the information being shown if data in not available.
In my view.rb file, I have something like this
<% if #content != nil %>
<div>
<h3>....</h3>
<% #content[0..3].each do |something| %>
<li> .... <li>
<% end %>
<% #content[4..5].each do |something| %>
<li> .... <li>
<% end %>
<% #content[5..11].each do |something| %>
<li> .... <li>
<% end %>
<div>
<% end %>
how ever even if content is nil, lines like content[4..5].each do |something| is being run and throwing errors for obvious reasons.
How do I get multiple blocks of html and ruby code to be ignored if condition isn't being met?
If the code <% content[4..5].each do |something| %> is executed, then #content IS NOT nil, there is no way ruby could be wrong about that.
BUT, if it is something like an empty array or a blank string then it will pass the test. In order to cover a wide range of possible "nil-like" values (nil, empty, blank...) use:
<% unless #content.blank? %>
And let me know if it helps.
you can use:
<% unless #content.nil? %>
#true
<%else%>
#false
<%end%>
Are you sure #content is surely nil? Please be aware that an object can be empty while it is not nil.
Eg:
#content=[]
or
#content={}
will make hold true for #content!=nil since it is not actually not nil. A reference is nil if it is never instantiated before.
If you are storing data to #content via a database query like Modelname,All , then you will get an empty array instead of a null (nil) object.
Also, if you are recieving #content from user as request parameter, then if the GET request contains name of the parameter but not any value, then again #content is empty but not nil.
ie http://yourdomain.com/somecontroller/index?name=10&age=
will have params[:age] as empty nil.
Solution:
<% if #content.to_s.empty? != true %>
<div>
<h3>....</h3>
<% content[0..3].each do |something| %>
<li> .... <li>
<% end %>
<% content[4..5].each do |something| %>
<li> .... <li>
<% end %>
<% content[5..11].each do |something| %>
<li> .... <li>
<% end %>
<div>
<% end %>
Related
For some reason my link_to_if line works, but appears on every show view for my model (Company).
Here's the code:
<% #customers.each do |customer| %>
<li>
<%= link_to_if customer.company_id == #company.id, "#{customer.first_name} #{customer.last_name}", customer_path(customer[:id]) %>
</li>
<% end %>
The issue: I have Customer1 linked to CompanyX. When I go to CompanyZ it shows Customer1, but the link is not a hyperlink. it's just plaintext, and not even supposed to be showing up. However on CompanyX's view, the link works fine. What am I doing wrong here?
If you read the documentation of link_to_if (https://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to_if), it clearly says that [if false] only the name is returned.
In the doc you can find that the (optional) block given is rendered in the false case. So in your case you could pass it an empty block:
<%= link_to_if false, customer_path(customer[:id]) {} %>
In my opinion, if you want to display the link only if one or more customer(s) from #customers are associated to that #company, you should do it this way:
<% #customers.where(company_id: #company.id).each do |customer| %>
<li>
<%= link_to "#{customer.first_name} #{customer.last_name}", customer_path(customer[:id]) %>
</li>
<% %>
if you want to hide some records you can do from from controller to control customers based company
#customers = Company.find(:id).customers
then in your views you can just show it without to compare it
<% #customers.each do |customer| %>
<li>
<%= link_to "#{customer.first_name} #{customer.last_name}", customer_path(customer[:id]) %>
</li>
<% end %>
How to make this code work?
<%= articles= Article.find_each
if articles
a.each do |a| %>
****some html****
<% end %>
<% end %>
right now it gives me an error:
no block given (yield)
It's hard to tell because your code is such a mess but i think you are trying to do this:
<% Article.all.each do |article| %>
<!-- some html - reference the local variable `article` in here, inside erb tags, eg -->
<div>
<%= article.name %>
</div>
<% end %>
EDIT: the above code will work fine (by which i mean happily generate no html at all) if there are no Article records in the db. Sometimes in this situation you might want to display some sort of extra info, like "You haven't created any Articles yet" or something. if this is the case you could do something like this:
<!-- typically this variable would be defined in the controller -->
<% #articles = Article.all %>
<% if #articles.blank? %>
<p>You haven't created any Articles yet</p>
<% else %>
<% Article.all.each do |article| %>
<!-- some html - reference the local variable `article` in here, inside erb tags, eg -->
<div>
<%= article.name %>
</div>
<% end %>
<% end %>
I'm using the each do loop correctly, and not getting errors when looping an active record base. But for some reason, I am getting extra information at the end.
Here's what my controller looks like:
def archivedBlogs
#compsci = Compsci.all
#personalb = Personalb.all
end
And here is the code I have in the view page:
<div class="panel">
<ul>
<%= #compsci.each do |blog| %>
<li><%= blog.title %></li>
<% end %>
</ul>
</div>
But as you can see, I'm getting extra stuff at the end:
How can I fix it so that it only prints the blog titles?
Change
<%= #compsci.each do |blog| %>
to
<% #compsci.each do |blog| %>
Here is my view:
<div>
<ul>
<%= #album.photos.each do |photo| %>
<li><%= link_to(image_tag(photo.soure.url(:small)),photo.source.url(:medium)) %></li>
<% end %>
</ul>
</div>
produces the right result except all the object ids (i.e. #<Photo:0xXXXXXX>#) get added right before the </ul> and display in the html. I'm guessing since each time the block gets executed it returns the Photo object and that's why its rendering all the #<Photo:0x>s but i don't know how to STOP this from happening.
It's because you have:
<%= #album.photos.each do |photo| %>
instead of:
<% #album.photos.each do |photo| %>
I've noticed that in some lines of rails views, this is used:
<% # Code... -%>
instead of:
<% # Code... %>
What is the difference?
<ul>
<% #posts.each do |post| -%>
<li><%=post.title%></li>
<% end -%>
</ul>
There will be no new lines in between the <ul> and first <li> and the last closing </li> and </ul>. If the - was omitted, there would.
The different options for evaluating code in ERB are as follows (they can be accessed in Textmate using Ctrl-Shift-. ):
<% %> Just evaluate the contents.
<%= %> Evaluate the contents and puts the result.
<%= -%> Evaluate the contents and prints the result.
<%# %> The contents is treated as a comment and not outputted.
Notice the difference between puts and print. Puts always adds a new line at the end of the string whereas print doesnt.
Basically, the -%> says don't output a new line at the end.
Consider this
<div>
<% if #some_var == some_value %>
<p>Some message</p>
<% end %>
</div>
The code above yields to the HTML below if the #some_var is some_value
<div>
<p>Some message</p>
</div>
If you've put - in the closing tag, then the ERB interpreter would remove the new lines for those with code tag including - and result in the following
<div>
<p>Some message</p>
</div>
This is useful if you need to have a good looking code for HTML. Sometimes you'll find it useful when working sideby side with a designer
Hope this helps.
A little late, but I think it's worth pointing out that you can also do this:
<%- #posts.each do |post| -%>
<li><%= post.title %></li>
<%- end %>
This strips away any whitespace in front.