lordstingray
lordstingray

Reputation: 31

Why is it keep saying undefined method?

Two models are created....and the associations are also mentioned in models.

first model is Item and second model is Activity.

Item model code:

class Item < ActiveRecord::Base
    has_many :activities
end

Activity Model code:

class Activity < ActiveRecord::Base
    belongs_to :item
end

i can show every item with the help of the link.....now i want to an activity to that specific item and for that purpose i have created a controller which is "Activities" and then i have written the code in it which is as follow:

Activities Model Code:

class ActivitiesController < ApplicationController
    def create
    @item = Item.find(params[:item_id])
    @activity = @item.activities.create(activity_params)
    redirect_to items_path(@item)
  end

  private
    def activity_params
      params.require(:activity).permit(:task)
    end
end

the code for the show.html.erb for the items contorller is as follow:

Show.html.erb(Items_controller) Code:

<h1>Show Goal</h1>

<p>
  <strong>Goal: </strong>
  <%= @item.goal %>
</p>

<p>
  <strong>Description: </strong>
  <%= @item.description %>
</p>

<h2>Activity</h2>
<% @items.activities.each do |activity| %>
  <p>
    <strong>Activity:</strong>
    <%= activity.task %>
  </p>

<% end %>

<h2>Add a Activity:</h2>
<%= form_for([@item, @item.activities.build]) do |f| %>
  <p>
    <%= f.label :Task %><br>
    <%= f.text_field :task %>
  </p>
    <%= f.submit "Add Activity"%>
  </p>
<% end %>


<%= link_to 'Edit', edit_item_path(@item) %>
<%= link_to  "Back" , items_path %>

The error comes from this line and it says "undefined method `activities' for nil:NilClass"

<% @items.activities.each do |activity| %>

Migration code for the "Activity" Model

Migration for Activity model Code:

class CreateActivities < ActiveRecord::Migration
  def change
    create_table :activities do |t|
      t.string :task


      t.timestamps null: false
    end
  end
end

Now i can not figure out where is this error coming from "undefined method `activities' for nil:NilClass"....????????

Upvotes: 0

Views: 166

Answers (4)

Sumeet Masih
Sumeet Masih

Reputation: 607

Error says "undefined method activities' for nil:NilClass" and which is right as well @items is nil

Go back to controller its @item, there is a typo

Additionally check your migration file, in migration file for Activity there should be a foreign key for Item item_id, if no drop that migration using

rake db:migrate:down VERSION=migration number

and edit the file and re run the rake db:migrate

Upvotes: 1

ydaniju
ydaniju

Reputation: 400

You need create a migration to add references for item to activity

rails g migration AddItemToActivities item:references

Then your file should look like this:

class AddItemToActivities < ActiveRecord::Migration
  def change
    add_reference :activities, :item, index: true
  end
end

Then you can easily loop by referencing this way:

<% @item.activities.each do |activity| %>
...

Upvotes: 0

Jayaprakash
Jayaprakash

Reputation: 1403

Replace the below line to

<% @items.activities.each do |activity| %>

to

<% @item.activities.each do |activity| %>

Upvotes: 0

Fallenhero
Fallenhero

Reputation: 1583

@items is nil because you define only @item

Upvotes: 1

Related Questions