Why Rails images display #<Photo:0xB4F28FA># object id? - ruby-on-rails

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| %>

Related

Render an attributes of each object - Rails

I have a view right now that renders an object on the page. The object is an Integration. On the Integration object I have attribute called filters. Filters are stored as an array. All I need to do is list out the filters of each integration below them in a list. Here is my code.
View
<% if #integrations.any? %>
<div class="configured-integrations">
<h3 class="heading-3">My Configured Integrations:</h3>
<ul class="integration-list integration-list--compact">
<%= render #integrations %>
</ul>
</div>
<% end %>
Screenshot
In the screenshot you can see that each of those elements are integrations. I need to list the filters of each integration below the title there.
Controller
def index
# Get the list of the user's integrations grouped first by provider then
# from oldest to newest."
#integrations = current_account.integrations
.order(type: :asc, created_at: :asc)
end
I hope this is clear enough. So recap: I need to list the filters on each integration below. I've already tried stuff like this #integrations.first.filters but that wont work because it's a static call. I need something like a list. Thank you
You can add another partial to render all filters which are associated with your Integration.
Create a partial file _show_filters.html.erb in your views
<% filters.each do |filter| %>
<li><%= filter %></li>
<% end %>
And render this partial while iterating through your #integration object like this.
<% if #integrations.any? %>
<div class="configured-integrations">
<h3 class="heading-3">My Configured Integrations:</h3>
<ul class="integration-list integration-list--compact">
<% #integrations.each do |integration| %>
<li>
<%= integration %>
<ul class="">
<%= render 'show_filters', filters: integration.filters %>
</ul>
</li>
<% end %>
</ul>
</div>
<% end %>
What you need to iterate through each integration, then <%= render integeration.filters %>
<% if #integrations.any? %>
<div class="configured-integrations">
<h3 class="heading-3">My Configured Integrations:</h3>
<ul class="integration-list integration-list--compact">
<% #integrations.each do |integration| %>
<li>
<%= integration %>
<ul class="">
<%= render integration.filters %>
</ul>
</li>
<% end %>
</ul>
</div>
<% end %>
You will have to update this code to make the partials work, but i hope this gets the idea across.
You can't use the shortcut <%= render #integrations %> here, because you want a subgroup inside #integrations. So you'll have to do it the long way.

Instagram gem returns full hash after iterating through it

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.

Getting extra information when looping in Ruby

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| %>

not displaying several html blocks if condition not met in ruby

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 %>

jruby issue iterating over multi dimensional hash object

So being new to rails I seem to be stuck on creating a loop within a loop to process the information.
I am getting:
can't convert Symbol into Integer line #11
The line in question is:
Host <%= servicedetails[:hostidn] %> - <%= servicedetails[:status] %>
And here is the full version below. Being new Im clueless and open to suggestions.
<div>
<% #service_hash[:service_list].each do |servicesinfo| %>
<ul>
<li>
<ul>
<li>
<h2><%= servicesinfo[:service_name] %><h2>
</li>
<% servicesinfo.each do |servicedetails| %>
<li>
Host <%= servicedetails[:hostidn] %> - <%= servicedetails[:status] %>
</li>
<% end %>
</ul>
</li>
</ul>
<% end %>
</div>
the JSON equivalent of this hash is
{"status":"successful","service_list":[{"service_name":"oozie","status":"RUNNING","status_message":"Running Master Service","host":"1"},{"service_name":"single-namenode","status":"RUNNING","status_message":"Running Service","host":"1"},{"service_name":"single-database","status":"RUNNING","status_message":"Running Service","host":"1"},{"service_name":"datanode","status":"RUNNING","status_message":"Running Service","host":"1"},{"service_name":"secondarynamenode","status":"RUNNING","status_message":"Running Service","host":"1"},{"service_name":"web","status":"DEAD","status_message":"Running Master Service","host":"1"},{"service_name":"tasktracker","status":"RUNNING","status_message":"Running Service","host":"1"},{"service_name":"jobtracker","status":"RUNNING","status_message":"Running Master Service","host":"1"}]}
You're already iterating over the array of hashes with service info (renamed to make sense):
<% #service_hash[:service_list].each do |service_info| %>
...
<% end %>
Iterating over service_info would return [key, value] pairs--likely not what you want.
Access the information from service_info directly, as you already do with :name
<%= service_info[:host] %> - <%= service_info[:status] %>
I don't see anything called :hostidn in that hash, just :host; not sure if that's a typo, or if you're expecting additional data not shown.

Resources