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| %>
Related
I'm using the following code to display posts to my users.
_feed.html.erb partial:
<% #posts_by_month.each do |monthname, posts| %>
<%= monthname %>
<ul>
<% posts.each do |post| %>
<li><%= post.created_at %></li>
<% end %>
</ul>
<% end %>
Controller:
def home
if logged_in?
#post = current_user.posts.build
#posts_by_month = current_user.feed.group_by { |post| post.created_at.strftime("%B") }
This renders my posts as follows:
Post 1
Post 2
Post 3
Post 4
I want to change it so that the posts are displayed like:
Post 1 Post 2 Post 3
Post 4 etc etc
etc
I've tried several approaches to this, including the in_groups_of(3) method however the way it is currently setup means nothing works. I feel like there is an obvious solution I'm missing - can anyone help?
[Edit to expand on the in_groups_of(3) error]
If I change line 4 in the _feed partial to:
<% posts.in_groups_of(3, false).each do |post| %>
It gives the error: undefined method `created_at' for #< Array:0xbb8f258 >
The #in_groups_of method returns an Array of Arrays each containing 3 Post objects.
So you now also need to iterate over the returned array that contains your three Posts, something like:
<% posts.in_groups_of(3, false).each do |post_group| %>
<% post_group.each do |post| %>
<li><%= post.created_at %></li>
<% end %>
<% end %>
You can use facets gem. This provides and each_by method. You can use each_by to create groups and iterate further on these groups.
Here is code snippet on how to use each_by
<div class = "small-9 columns vertical-border-left">
<%- #client.contact_details.each_by(3) do |contact_details| %>
<div class="row">
<%- contact_details.each do |contact| %>
<div class="small-3 columns small">
<div> <%= contact.contact_detail_type %> contact </div>
<div> <%= contact.contact_email %> </div>
<div> <%= contact.contact_phone %> </div>
</div>
<% end %>
</div>
<% end %>
</div>
I am using the instagram gem with rails. My problem is I am iterating over all of the data in the response with code like this:
<div>
<ul>
<%= #instagram.each do |pic| %>
<li><%= pic.name %></li>
<li><%= pic.latitude%></li>
<li><%= pic.longitude%></li>
<% end %>
</ul>
</div>
I am getting the desired results except for the end, where this gets printed in the browser:
[#<Hashie::Mash id="152201866" latitude=45.89172 longitude=-64.370013 name="Marshview Middle School">, ... etc...]
Any help trying to figure out how to ignore that last bit would be greatly appreciated.
Change
<%= #instagram.each do |pic| %>
to
<% #instagram.each do |pic| %>
= expects a return and prints the collection it iterates over.
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 %>
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| %>
A first look I thought erb accepts any Ruby code, but I've got this strange behaviour...
I have an array [of tags for my article], and I want to make a nice display for them. So I'm writing something like this:
<ul>
<% #post.tags.each do |item| %>
<li>item</li>
<% end %>
</ul>
The wrong output looks like this:
<ul>
<li>item</li>
<li>item</li>
<li>item</li>
...
</ul>
Where I am wrong? Any suggestions how to make a proper iteration?
You forgot the <%= %> to display the value of item:
<ul>
<% #post.tags.each do |item| %>
<li><%= item %></li>
<% end %>
</ul>