Associations I - Exercise 5 - Show a Tag - Undefined Method error


#1



Associations I - Exercise 5 - Show a Tag - Undefined Method error

I'm running this exercise in a local environment due to the pervasive "Not Found" error in the environment. Any help is appreciated!


https://www.codecademy.com/en/courses/learn-rails/lessons/one-many/exercises/one-many-show-tag

When I click on the "Learn More" tag below the destination image in the #index screen I get the error:

  NoMethodError in TagsController#show
 undefined method `destinations' for #<Tag:0x007ffaa07a8440>

My tags_controller.rb:

class TagsController < ApplicationController
	def index
		@tags = Tag.all
	end

  def show
    @tag = Tag.find(params[:id])
    @destinations = @tag.destinations
  end

end

and the show.html.erb

<div class="header">
  <div class="container">
    <img src="http://s3.amazonaws.com/codecademy-content/courses/learn-rails/img/logo-1tm.svg" width="80">
    <h1>BokenjiKan</h1>
  </div>
</div>

<div class="tag">
  <div class="container">
    <h2 class="tag-title"><%= @tag.title %>  </h2>

    <div class="cards row">

      <% @destinations.each do |d| %>

      <div class="card col-xs-4">
        <%= image_tag d.image %>
        <h2><%= d.name %></h2>
        <p><%= d.description %></p>
      </div>

      <% end %>

    </div>

  </div>
</div>


#2

Hi @rubyrockstar65407,

Could you please post both of your db/migrate files? You need to have a belongs_to column in your destinations migration file that will connect it to the tags.


#3

Here is the schema and the migrate files:

ActiveRecord::Schema.define(version: 20170613232129) do

  create_table "destinations", force: :cascade do |t|
    t.string "name"
    t.string "image"
    t.string "description"
    t.integer "tag_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["tag_id"], name: "index_destinations_on_tag_id"
  end

  create_table "tags", force: :cascade do |t|
    t.string "title"
    t.string "image"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

end

class CreateDestinations < ActiveRecord::Migration[5.1]
  def change
    create_table :destinations do |t|
      t.string :name
      t.string :image
      t.string :description
      t.references :tag
      t.timestamps
    end
  end
end

class CreateTags < ActiveRecord::Migration[5.1]
  def change
    create_table :tags do |t|
      t.string :title
      t.string :image
      t.timestamps
    end
  end
end

#4

I did delete the db and recreate it but I did not run the migrations after the recreate.


#5

@rubyrockstar65407 I'm not sure where you got some of the stuff there, it definitely wasn't in the course. You'll need to migrate the database though, so please try that and let me know if it works.


#6

I'm running all of this in a local environment, but all of the code came from the exercise. What part does not look Code Academy standard?


#7

zystvan, I ran the migrations and restarted the app. The same error appears

J


#8

After the migration i get this error in the console:

Started GET "/tags" for 127.0.0.1 at 2017-06-20 12:07:19 -0500
   (0.2ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
Processing by TagsController#index as HTML
  Rendering tags/index.html.erb within layouts/application
  Tag Load (0.6ms)  SELECT "tags".* FROM "tags"
  Rendered tags/index.html.erb within layouts/application (9.3ms)
Completed 200 OK in 236ms (Views: 212.6ms | ActiveRecord: 1.2ms)


Started GET "/tags/1" for 127.0.0.1 at 2017-06-20 12:07:22 -0500
Processing by TagsController#show as HTML
  Parameters: {"id"=>"1"}
  Tag Load (0.4ms)  SELECT  "tags".* FROM "tags" WHERE "tags"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
Completed 500 Internal Server Error in 16ms (ActiveRecord: 0.4ms)


  
NoMethodError (undefined method `destinations' for #<Tag:0x007f931eaa2248>):
  
app/controllers/tags_controller.rb:8:in `show'

#9

At exercise 5 in this lesson, my db/migrate files look like this:

db/
|-migrate/
  |-20150408005237_create_tags.rb
  |-20150408005247_create_destinations.rb

And the contents of those files:

20150408005237_create_tags.rb

class CreateTags < ActiveRecord::Migration
  def change
    create_table :tags do |t|
      t.string :title
      t.string :image
      t.timestamps
    end
  end
end
20150408005247_create_destinations.rb

class CreateDestinations < ActiveRecord::Migration
  def change
    create_table :destinations do |t|
      t.string :name
      t.string :image
      t.string :description
      t.references :tag
      t.timestamps
    end
  end
end

Try orienting your files more similarly to mine. The exercises don't cover anything about forcing cascades, and you shouldn't be manually creating columns like t.integer "tag_id". Use the built in Rails helpers instead, such as t.references :tag.


#10

Z,

Thanks for looking at the code. My migrate files are exactly like yours with the exception of 'ActiveRecord::Migration[5.1]' vs. 'ActiveRecord::Migration'. I have not added the t.integer "tag_id" code manually to the schema. I suspect is may be because of Rails version differences. I've dropped and recreated the tables but the result is the same. My code is at: https://github.com/jeffraun/associations_I

Jeff


#11

Thanks for putting it on GitHub, that makes it way easier to find the problem :slight_smile:

I think there may be a problem with your models now. class [Thing] < ActiveRecord::Base is the Rails 4 syntax for a model, but since you're using Rails 5 it's class [Thing] < ApplicationRecord. Looking at your Tag model, I can see that you tried to use both, rather than just one. You'll need to make remove lines 2 and 4, so it looks similar to your Destination model.


#12

Z!!

Thanks! I updated the tag model with your updates and it works!!!

Jeff


#13

This topic is solved.