5.3 show a tag : undefined method `belongs_to' for main:Object


#1

need some help. can someone tell me what's wrong with my code and why .

https://www.codecademy.com/courses/learn-rails/lessons/one-many/exercises/one-many-show-tag?action=resume


#2

Hi @je22,

Could you please copy/paste in your code for the file that's throwing the error? It's not the one you posted, since there's no belongs_to anywhere in that file.


#3

hi zystvan

here are all the files i'va worked on but i don't see any belong_to,in one of them. maybe i should change my glasses

tags_controller :

class TagsController < ApplicationController
	def index
    @tags = Tag.all
  end
  def show 
  @tag = Tag.find(params[:id]) 
  @destinations = @tag.destinations 
end
end

route.rb :

Rails.application.routes.draw do
	get '/tags' => 'tags#index'
  get '/tags/:id' => 'tags#show', as: :tag
end

index.html :

<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="tags">
  <div class="container">
    <div class="cards row">
    
<div class="cards row">
  <% @tags.each do |t| %>
  	<div class="card col-xs-4">
	<%= image_tag t.image %>
	<h2> <%= t.title %> </h2>
  	</div>
  <% end %>
</div>      <!--
      To do: Loop through tags and display each one with this HTML
      <div class="card col-xs-4">
        tag's image goes here
        <h2> tag's title goes here </h2>
      </div>
      -->

    </div>
  </div>
</div>

and finally 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 |t| %>
      <div class="card col-xs-4">
        <%= t.image %>
        <h2> <%= t.name %> </h2>
        <p> <%= t.description %> </p>
      </div>
      <% end %>

    </div>

  </div>

thank you for your help


#4

by the way here is the link to this lesson. hope this wil be helpfull ?

https://www.codecademy.com/fr/courses/learn-rails/lessons/one-many/exercises/one-many-show-tag?action=resume


#5

that 's what i've got in the console


#6

and if i change tags by show that's what i've got


#7

@je22 OK, I think that there are two separate problems here, so we'll try to fix them one by one. Could you copy/paste in tho code you have for app/models/tags.rb and app/models/destinations.rb? I'm guessing that the problem is in one of those.

If neither of those files has “belongs_to” in it, you can enter this in the terminal:

$ grep -r "belongs_to"

and look at the files it shows, to see if any of those are ones you've modified, and if they are you can show me those files instead :slight_smile:


#8

hi Zystvan
here are the file you ask for :

tags.rb :

class Tag < ActiveRecord::Base
end
class Tag < ActiveRecord::Base
has_many :destinations
end

and the other one destinations.rb :

class Destination < ActiveRecord::Base
end
belongs_to :tag

in fact there is a "belong_to" in this one and i don't know why !


#9

@je22 Try putting your end on the very last line, so that destinations.rb looks like this instead:

class Destination < ActiveRecord::Base
  belongs_to :tag
end

And that should fix that error so we can move on to the next problem :slight_smile:


#11

hi Zystvan

in fact i surely was to tired not to see the "end" was'nt in an appropriate place and that solved the "belond_to' problem


#12

now i've got a wrong number of argument error message in the app/tags/show.html.erdb


Why am I Getting This Error?
#13

@je22 Could you post your code for that file?


#14

A post was split to a new topic: Why am I Getting This Error?


#15

hi zystvan sorry to answer you so lately but i've been a little bit seek

here is the code you ask for :

<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 |t| %>
      <div class="card col-xs-4">
        <%= t.image %>
        <h2> <%= t.name %> </h2>
        <p> <%= t.description %> </p>
      </div>
      <% end %>

    </div>

  </div>

#16

@je22 Try replacing tag.title with @tag.title, and see if that does it.

Otherwise, if you're still getting the same error message, is there a line it highlights?


#17

ok that was it thanks a lot


#18

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.