Home>

I have a question.

The following error has occurred.

ArgumentError in Nutritions # index
Showing /Users/shotaro_hosoda/projects/berries/app/views/nutritions/index.html.erb where line # 32 raised:

wrong number of arguments (given 1, expected 2)

I presume that the part where this error is occurring is as follows.

[View file] index.html.erb

Omission
<% @ nutritions.each do | nutrition |%>
<tr height = "60">
<% if user_signed_in?%>
 <% if current_user.already_favorited? (Nutrition)%>
  <td><% = nutrition.ingredient%>
 <% else%>
  <td><% = nutrition.ingredient%>
 <% end%>
<% else%>
 <td><% = nutrition.ingredient%>
<% end%>
  
 <ul>
   <li>
<% if user_signed_in?%>
<% if current_user.already_favorited? (Nutrition, current_user)%>←  Error part
  <% = link_to'Release', nutrition_favorites_path (user_id: current_user.id, nutrition_id: nutrition.id, id: 0), method :: delete%>
<% else%>
  <% = link_to'Registration', user_favorites_path (user_id: current_user.id, nutrition_id: nutrition.id), method :: post%>
<% end%>
<% = link_to'edit', edit_nutrition_path (nutrition.id), method:: get%>
<% = link_to'Delete', nutrition_path (nutrition.id), method:: delete, data: {confirm:'Are you sure I want to delete? '}%>
<% end%>
Omission

[Model] user.rb

#Include default devise modules. Others available are:
  #: confirmable,: lockable,: timeoutable,: trackable and: omniauthable
  devise: database_authenticatable,: registerable,
         : recoverable,: rememberable,: validatable
  has_many: nutritions
  has_many: favorites, foreign_key: true, dependent:: destroy
  has_many: fav_nutritions, through:: favorites, source:: nutrition
  def already_favorited? (nutrition, current_user)
    Favorite.exists? (User_id: current_user.id, nutrition_id: nutrition.id)
  end
end
code


The already_favorite? method is called in the error part of the view.
At that time, two arguments (nutrition, current_user) are passed, and two formal arguments of the method are also defined (nutrition, current_user).

So why do we get the wrong number of arguments (given 1, expected 2)?
Since it says given1, does it mean that one of the (nutrition, current_user) at the time of calling is incorrect?

I would appreciate it if you could teach me.

  • Answer # 1

    The following has only one argument.

    <% if user_signed_in?%>
     <% if current_user.already_favorited? (Nutrition)%><--- Here
      <td id = "ingredient-column">★<% = nutrition.ingredient%>
     <% else%>