Thinkerer
Thinkerer

Reputation: 1626

undefined method `model_name' for ActiveRecord::Relation::ActiveRecord_Relation_Link:Class

I'm trying to follow some lessons on creating a list type of forum.

Now the problem is to the Destroy function:

undefined method `model_name' for ActiveRecord::Relation::ActiveRecord_Relation_Link:Class

For this line in show.html.erb (app/views/links/show.html.erb where line #25 raised:)

<td><%= link_to 'Destroy', @link, :confirm => 'Are you sure?', :method => :delete %></td>

Links controller

Class LinksController < ApplicationController

    def show
        @link = Link.all        
        #@link = Link.find(params[:id])
    end    

    def new
        @link = Link.new
    end 

    #added from others 
    def edit 
        @link = Link.find(params[:id])
    end    

    def destroy
        @link = Link.find(params[:id])
        if @link.destroy
            redirect_to @link, :notice => 'Link successfully deleted'
        else 
            render show: 'action'
        end 
    end     

    def create
        @link = Link.new(link_params)
        @link.user_id = current_user.id
        #@link.save
        if @link.save
            redirect_to @link, :notice => 'Link successfully created'
        else 
            render action: 'new'
        end 
    end 

    private

    def link_params
        params.require(:link).permit(:url, :title) #need to add :user_id?
    end    
end

Snippets from the show.html.erb

<%= @link.each do |link| %>
<%#link_to(link.title, link.url) %>
    <tr>
        <td><%= link.user_id %></td>
        <td><%= link_to link.title, link.url %></td>
        <td><%#= link.description %></td>
        <td><%= link_to 'Show', link %></td>
        <td><%= link_to 'Edit', edit_link_path(@link) %></td>
        <td><%= link_to 'Destroy', @link, :confirm => 'Are you sure?', :method => :delete %></td>

This is the routes.rb

Project::Application.routes.draw do
  devise_for :users
  resources :pages
  root :to => "pages#index"
  resources :links
End

Separately, theres another issue.

The resulting links page also throws up the entire Link database in gibberish form as well as the table form I displayed.

Upvotes: 2

Views: 1141

Answers (2)

Santhosh
Santhosh

Reputation: 29124

Change @link to link inside the loop

<%= @link.each do |link| %>
  <tr>
    <td><%= link.user_id %></td>

    ..
    <td>
      <%= link_to 'Destroy', link, :confirm => 'Are you sure?', :method => :delete %>
    </td>

link_to 'Destroy', link is shortcut for link_to 'Destroy', link_path(link)

@link, in this case is Link.all, but what you need is the local variable link which is the individual Link object

Upvotes: 1

Mandeep
Mandeep

Reputation: 9173

In your show action you have

def show
  @link = Link.all  # this is commented out in your code     
  #@link = Link.find(params[:id])
end

Your @link is nil because you have commented it out, uncomment it and it will show up. Also if you want to show all the links then you should use index action not show. Show action should be use for showing that particular link

Edit:

According to your comment your index action should be like this:

def index
  @links = Link.all
end

and show action should be

def show
  @link = Link.find(params[:id])
end

and your html code should be in index.html.erb

<%= @links.each do |link| %>
  <%#link_to(link.title, link.url) %>
  <tr>
    <td><%= link.user_id %></td>
    <td><%= link_to link.title, link.url %></td>
    <td><%#= link.description %></td>
    <td><%= link_to 'Show', link %></td>
    <td><%= link_to 'Edit', edit_link_path(@link) %></td>
    <td><%= link_to 'Destroy', link, :confirm => 'Are you sure?', :method => :delete %>      </td>
  </tr>
<% end %>

Note the destroy link i have used. I have used link and not @link as you are inside a loop and you have access to your link by link and not @link

Upvotes: 3

Related Questions