Confused about nil rails view - ruby-on-rails

I'm simply trying to ensure a value exists in a certain block and to display it's contents if it does and if not display "Nothing for this yet." I've researched other SO posts but my implementation must be off.
<% #profiles.each do |profile| %>
<%= profile.current_club %>
<% if profile.listings %>
<%= video_thumb_embed(profile.listings.last.video).html_safe %>
<% end %>
<% end %>
Video is an attribute of the listings model.
I'm trying to display all user profiles on an index page and to display the latest video they have added on this index page.
It errors out if a user hasn't added a listing so I added what I thought would be a simple check in the each block but it errors out with the below
undefined method `video' for nil:NilClass
What am I not understanding here? With the above I expect a profile would show on the index page with a video if it had one and if it didn't it wouldn't display anything.

You want to check and make sure that profile.listings is not blank or empty. If you call the last method on an empty array, it returns nil.
<% if profile.listings %> will return true even if you have an empty array.
<% if !profile.listings.blank? %> or <% unless profile.listings.blank? %> should do the trick.

Related

How do I add an image placeholder to fix this bug

I am a beginner programmer. I'm building my first project which shows a list of my favorite athletes and when clicked the "show" page loads an image of them, their name, and weight.
Overall when adding a new athlete everything works fine. If I decide to add a new athlete and skip adding an image I get this error "Can't resolve image into URL: undefined method `persisted?' for nil:NilClass", it also highlights my code "<%= image_tag(#fighter.image) %>" that's in my show.html.erb
I've tried adding an alt: but I get another error of "wrong number of arguments (given 1, expected 0)".
I've searched for answers and one thread mentioned my controller page and the params section. I did notice that (:image) is preceded by "params.require" which I would assume could be the problem if I'm not adding an element that is required. How do I solve this issue? What would be the best way to add a placeholder, I've added a default.png in my images folder.
Thank you
show.html.erb
fighters_controller.rb
If you're using ActiveStorage for uploading athlete images. You can check if an image was attached to your model with #fighter.image.attached?.
So I would suggest wrapping the image_tag in an IF-clause like so:
<% if #fighter.image.attached? %>
<%= image_tag(#fighter.image) %>
<% else %>
<%= image_tag("default.png") %>
<% end %>
If you're using Paperclip then try the following:
<% if #fighter.image.exists? %>
<%= image_tag(#fighter.image) %>
<% else %>
<%= image_tag("default.png") %>
<% end %>
With Carrierwave use:
<% if #fighter.image.present? %>
<%= image_tag(#fighter.image) %>
<% else %>
<%= image_tag("default.png") %>
<% end %>

Rails - how to write an index view?

I'm having trouble figuring out how to display an index.
In my organisation requests view folder, I have a file called index.html.erb.
In that file, I'm trying to list each organisation request. I've tried each of the following formulations:
<% OrganisationRequest.each do |OrgReq| %>
<% organisation_request.each do |OrgReq| %>
<% #organisation_request.each do |OrgReq| %>
<% #organisation_requests.each do |OrgReq| %>
In each case, I get an error that says:
formal argument cannot be a constant
I thought a constant meant something beginning with a capital letter. 3 of the above attempts don't begin with a capital letter.
It's also confusing to me since in my user index, I have <% User.each %> and I don't get an error message.
Can anyone see what's gone wrong? How do I ask for a list of objects?
If you have your data and view right, you should be able to fix with:
<% #organisation_requests.each do |org_req| %>
...
<% end %>
If we stick Rails conventions, we'd say that, you have a OrganisationRequests controller, has such content.
class OrganisationRequestsController < ApplicationController
...
def index
#your_local_variable = OrganisationRequest.find(...)
end
...
end
That is to say, you need to use, #your_local_variable inside view file.
<% #your_local_variable.each do |o| %>
....
<% end %>
If the variable inside index action is #organisation_requests, use that.

Why am I getting an undefined method error in rails after I check if method exists?

On my user profile page in my rails app, I am recieving the error:
undefined method `title' for nil:NilClass
I know it is because of the following 3 lines of code:
<% if #user.profile.title %>
<%= #user.profile.title %>
<% end %>
I don't understand why. Since i use the if statement, shouldn't it first check whether title exists, then if it exists display it, and if it does not exist, it should not display it. What is wrong and how do I fix it? Thanks.
Since title is nil you can't test against it like you did.
Try:
<% if #user.profile.try(:title) #user.profile.title %>
You can do:
<% if #user.profile.present? %>
<%= #user.profile.title %>
<% end %>
Or with try:
<%= #user.profile.try(:name) %>
With a default value if no profile associated:
<%= #user.profile.try(:name) || 'No profile for this user' %>
Because it is not correctly. If you want to get access to some parameter from user you have to use direct access. But if you want to use profile that belongs to User model look at this
link

Displaying images from associated models

I'm trying to get my miniatures view to display all associated photos from the collections model.
My sample miniature has two photos but the following code gives an error "undefined method `photo' for #".
<% #miniature.collections(:photo).each do |photo| %>
<%= image_tag #miniature.collections.photo.url(:medium) %>
<% end %>
I think that the relationships are all correct though because rails console works fine with them and the following code shows the first image, twice.
<% #miniature.collections(:photo).each do |photo| %>
<%= image_tag #miniature.collections.first.photo.url(:medium) %>
<% end %>
Equally I can swap out first for last and show the 2nd image twice. What am I doing wrong?
Without knowing a huge amount about your associations, I'm relatively sure you want to use the instance variable that you instantiated in the each loop. That would look like something to the effect of:
<% #miniature.collections(:photo).each do |collection| %>
<% if !collection.photo.url(:medium).nil? %>
<%= image_tag collection.photo.url(:medium) %>
<% end %>
<% end %>
Using #miniature.collections.first.photo.url(:medium) would display the first image of the collection once for each photo in the collection. Likely this is not what you want.

Ruby How to display message if include? statement is true or false

I'm very new to Ruby and am attempting to display one of two messages based on whether certain words are included in an array. My current controller contains the following:
#cookie = ["gluten", "sugar", "dairy", "chocolate"]
And my view contains this:
<%= #cookie.include?"gluten" %>
The above returns 'true' and prints on the page just fine. However, nothing gets printed on the page when trying either of the following methods. The page renders fine with no errors, but no messages:
<%= puts "Sorry" if #cookie.include?"gluten" %>
and
<%=
if #cookie.include?("gluten")
puts "Sorry, you cannot eat this."
else
puts "You have the greenlight."
end
%>
I'm hoping that I'm creating a very simple mistake in syntax or am misunderstanding the usage of the include? function.
looking at the ruby it appears to be sound, http://rubyfiddle.com/riddles/d6798.
Must be rails error so try
<% if #cookie.include?("gluten") %>
<%= "Sorry, you cannot eat this." %>
<% else %>
<%= "You have the greenlight." %>
<% end %>
If you want it all in one erb statement, you would do something like this:
Don't use "puts". The "<%=" is already doing a 'to_s' and will output the value to the response body. You may have seen the results show up in your logs if you used 'puts'.
Also - for erb, the <% ... %> syntax, that is for logic, often an 'each' or 'if' state, the <%= %> is for writing to the screen. Most of the time it is the attribute of a ruby object like <%= user.name %>

Resources