How to fix error in rails - ruby-on-rails

This is the error I am encountering and I was wondering how I can fix it. I'm new to rails and I didn't think that .each was a method. If some one could please help me it would be very apreciated. thanks
undefined method `each' for nil:NilClass
this is the code that i am getting the error on.
<h1>Portfolio Items</h1>
<% #portfolio_items.each do |portfolio_item| %>
<p><%= portfolio_item.title %></p>
<p><%= portfolio_item.subtitle %></p>
<p><%= portfolio_item.body %></p>
<%= image_tag portfolio_item.thumb_image unless portfolio_item.thumb_image.nil? %>
<% end %>

Welcome to SO.
Either you have not set the var #portfolio_items or its mis-spelled.
If you have defined it then at least it should have had Array like data structure And prevented this error.
Some of the array like objects will be ActiveRecord::Associations::CollectionProxy
In ruby undefined #instance vars are set with nil and no error is raised.
If this does not help, please update your question with more details and I will update this answer.

If you do:
[nil].each{|e| p e}
=> nil
You can see there's an element inside the array, so you can use each, you can iterate and print its content, and you'll see there's a nil value inside.
If in the other hand you write:
nil.each{|e| p e}
You'll get the error:
undefined method `each' for nil:NilClass (NoMethodError)
That's to say there's no each method available between all the methods that can be applied to nil.
In your code, in the controller most probably, you're defining #portfolio_items, but for some reason, this isn't taking the value you want, it's getting a nil value, so when you try to iterate over it, you get that error.

You code is expecting #portfolio_items to be an array, but either #portfolio_items is not assigned a value or assigned a nil in the controller action.

your list is nil, you can do an if...else check to handle it on your view template
<h1>Portfolio Items</h1>
<% if #portfolio_items.count == 0 %>
<p>There are no items</p>
<% else %>
<% #portfolio_items.each do |portfolio_item| %>
<p><%= portfolio_item.title %></p>
<p><%= portfolio_item.subtitle %></p>
<p><%= portfolio_item.body %></p>
<%= image_tag portfolio_item.thumb_image unless portfolio_item.thumb_image.nil? %>
<% end %>
<% end %>

You can try use :
(#portfolio_items || []).each do |portfolio_item|

The instance variable #portfolio_items is nil and each loop over nil is throw error.
So follow below code:
<h1>Portfolio Items</h1>
<% if #portfolio_items.nil? %>
No Item to display.
<% else %>
<% #portfolio_items.each do |portfolio_item| %>
<p><%= portfolio_item.title %></p>
<p><%= portfolio_item.subtitle %></p>
<p><%= portfolio_item.body %></p>
<%= image_tag portfolio_item.thumb_image unless portfolio_item.thumb_image.nil? %>
<% end %>
<% end %>

Related

Index View Error wrong amount of arguments

I'm getting an argument error and I am not to sure why. I am still fairly new to using Rails and programming overall. I am currently working on my index page and am just trying to get the description displayed. Below is the view and controller. I am not sure why t.description is being expected to have more than one argument. Is this because of strong params?
View:
<h1> All Tea Blends </h1>
<% #teas.each do |t| %>
<h2><%= link_to t.flavor, tea_path(t.id)%> - <%= t.brand.name %></h2>
<% link_to "Write a review", new_tea_review_path%>
<% end %>
<div>
<p><%= t.description %></p>
</div>
Controller:
class TeasController < ApplicationController
def index
#teas = Tea.order_by_rating.includes(:brand)
end
end
t.description is outside of the loop. Not sure why you'd get wrong number of arguments (t is also used for translations, though). Just move it inside the loop...
<% #teas.each do |t| %>
<h2><%= link_to t.flavor, tea_path(t.id) %> - <%= t.brand.name %></h2>
<% link_to "Write a review", new_tea_review_path %>
<div>
<p><%= t.description %></p>
</div>
<% end %>
I'd also recommed changing your loop variable to tea instead of t. Its more descriptive and avoids conflicts.

How to specify syntax error in Ruby on Rails

My controller is like this
def show
#receipt = Receipt.find(params[:id])
#hospitalizations=#receipt.hospitalizations
#outpatients=#receipt.outpatients
#surgeries=#receipt.surgeries
end
my show.html.erb is like this.
<h1>details of receiptid: <%= #receipt.id %></h1>
<% #hospitalizations.each do |hospitalization| %>
<p>hospitalization_id:<%= hospitalization.id %>hospitalization_start :<%= hospitalization.hospitalization_start %> hospitalization_end:<%= hospitalization.hospitalization_end %> </p>
<% #surgeries.each do |surgery| %>
<p>surgeryid:<%= surgery.id %> surgery_day :<%= surgery.surgery_day %> </p>
<% #outpatients.each do |outpatient| %>
<p>outpatientid:<%= outpatient.id %>outpatient_day :<%= outpatient.outpatient_day %> </p>
<% end %>
When I access it's page,some error like below was incurred. I tried to specify error location,but didn't work well.
It seems syntax error,where should end insert?
If someone has experienced same issues,please let me know.
Each of your each loop requires an <% end %> clause, you can't find the specific line since the error says it reached the end when it expected "end". Close all your loops like this:
<% #hospitalizations.each do |hospitalization| %>
<p>hospitalization_id:<%= hospitalization.id %>hospitalization_start :<%= hospitalization.hospitalization_start %> hospitalization_end:<%= hospitalization.hospitalization_end %> </p>
<% end %>

undefined method `value' for #<CategoryItemValue::ActiveRecord_Associations_CollectionProxy:0x007ff9706d24c0>

This is so simple but isnt working. What am I missing?
controlelr
#guide = Guide.friendly.find(params[:guide_id])
#category = #guide.categories.friendly.find params[:id]
#items = #category.category_items
view
<% #items.each do |item| %>
<%= item.category_item_values.value %>
<% end %>
gives the no method error of
undefined method 'value' for #<CategoryItemValue::ActiveRecord_Associations_CollectionProxy:0x007ff9706d24c0>
There is a values column in the category_item_values table so I'm not sure what the problem is.
item.category_item_values is the CollectionProxy instance (one might think of it as of an kinda array.)
Each category_item has [likely, you did not provide sufficiently enough info to guess more precisely] many values. If the assumption above is correct, here you go:
<% #items.each do |item| %>
<% item.category_item_values.each do |value| %>
<%= value %> # or maybe (depending on your model) <%= value.value %>
<% end %>
<% end %>
You will have to loop over each of the category_item_values to get the result as this suggests <CategoryItemValue::ActiveRecord_Associations_CollectionProxy:0x007ff9706d24c0> that your category_item_value is a association.
So you could do something like
<% item.category_item_values.each do |category_item_value| %>
<%= category_item_value.value %>
<% end %>

How do i fix this if/else statement so there is no argument error

Hello I'm a beginner at rails and am trying to create a very basic Todo list application.
For my view for the show action for viewing an individual Todo list I am trying to write an if/else statement that when the complete_by date is empty (as some people might not want a specific list to have a complete by date) it states that there is no complete by date, whether if the complete_by had a date, it would state the date.
Here is my form for creating a new list:
and here is my code (in /app/views/lists/show.html.erb)
<h2><%= #list.title %></h2>
<% if #list.complete_by(presence: false) %>
<h4>This list has no completion date</h4>
<% else %>
<h4>To be completed by: <%= #list.complete_by %></h4>
<% end %>
<%= link_to 'Back', lists_path %>
I know that the <% if #list.complete_by(presence: false) %> line is incorrect, but how do I make it so that there will be no argument error like so:
Thank you for your time,
Brian
Simply use .nil? to check if a value is nil.
<% if #list.complete_by.nil? %>
<h4>This list has no completion date</h4>
<% else %>
<h4>To be completed by: <%= #list.complete_by %></h4>
<% end %>
You can also take advantage of the nil-to-false evaluation by swapping the conditions:
<% if #list.complete_by %>
<h4>To be completed by: <%= #list.complete_by %></h4>
<% else %>
<h4>This list has no completion date</h4>
<% end %>
or (a little bit less readable)
<% if !#list.complete_by %>
<h4>This list has no completion date</h4>
<% else %>
<h4>To be completed by: <%= #list.complete_by %></h4>
<% end %>

Rails syntax error: unexpected keyword_ensure, expecting keyword_end

<h1>Get Ready</h1>
<% if params[:ballot_position].to_i > 1 %>
<p>
Voter <%= params[:ballot_position].to_i - 1 %>, go get voter <%= params[:ballot_position] %>
and switch places with them.
</p>
<p>
Voter <%= params[:ballot_position] %>, when you are ready, click the button marked "Ready" below.
</p>
<% #ballot_link = "/vote/#{params[:election_id]}/ballot/#{params[:ballot_position]}" %>
Ready
Above code seems to be resulting in:
ready.html.erb:13: syntax error, unexpected keyword_ensure, expecting keyword_end
ready.html.erb:15: syntax error, unexpected $end, expecting keyword_end
What's going on? What's wrong with this syntax?
The errors you're receiving more than likely stem from trying to execute a if-else conditional wherein you have an extra <% end %> before <% else %>. Ensure that your conditional follows canonical if-else-end logic like the following:
<% if ... %>
<% #ballot_link = "/vote/#{params[:election_id]}/ballot/#{params[:ballot_position]}" %>
Ready
<% else %>
...
<% end %>
You are using if condition. So you should end it. The basic if conditions syntax for erb is
<% if ...condition.. %>
Statement
<% end %>
You have to decide what are you using ? It is if condition or if-else condition
In you case, there is not <% end %> clause at end so you have to add it.
<h1>Get Ready</h1>
<% if params[:ballot_position].to_i > 1 %>
<p>
Voter <%= params[:ballot_position].to_i - 1 %>, go get voter <%= params[:ballot_position] %>
and switch places with them.
</p>
<p>
Voter <%= params[:ballot_position] %>, when you are ready, click the button marked "Ready" below.
</p>
<% #ballot_link = "/vote/#{params[:election_id]}/ballot/#{params[:ballot_position]}" %>
Ready
<% end %> # this is what you have to add
My issue was that I forgot to end a do block when creating a link using link_to. My incorrect code looked like:
<%= link_to("#", :class => "example-class") do %>
Nested HTML goes here
I had forgotten to end the do statement. The correct code looks like:
<%= link_to("#", :class => "example-class") do %>
Nested HTML goes here
<% end %>
Hope this helps someone.

Resources