Agustin Seifert
Agustin Seifert

Reputation: 1968

Routing Error in Ruby on Rails "action: show"

i'm new with RoR and i'm trying to make a master-datail: User -Lugar(place)

My Routes.rb

  resources :users do
    resources :lugars
  end

my controller lugars

class LugarsController < ApplicationController 
  def create
    @user = User.find(params[:user_id])
    @lugar = @user.lugars.create(params[:lugar])
    redirect_to account_url
  end

  def destroy
    @user = User.find(params[:user_id])
    @lugar = @user.lugars.find(params[:id])
    @lugar.destroy
    redirect_to account_url
  end

  def edit
    @user = User.find(params[:user_id])
    @lugar = @user.lugars.find(params[:id])
  end

  def show
    @user = User.find(params[:user_id])
    @lugar = @user.lugars.find(params[:id])
  end

  def index
    @user = User.find(params[:user_id])
    @lugar = @user.lugars.all
  end
end

And my view

....
    <% @user.lugars.each do |lugar| %>
      <tr>
        <td><%= lugar.nombre %></td>
        <td><%= lugar.telefono %></td>
        <td><%= lugar.direccion %></td>
        <td><%= lugar.numero %></td>
        <td><%= lugar.localidad %></td>
        <td><%= lugar.provincia %></td>
        <td><%= link_to "Editar",  user_lugar_path %></td>
        <td><%= link_to "Eliminar", [lugar.user , lugar], :method => :delete %></td>
      </tr>
    <% end %>
....

I'd created and empty file in my lugars view "show.html.erb" but when I enter the page allways get:

Routing Error

No route matches {:action=>"show", :controller=>"lugars"}

Any idea? Thanks in advance

Upvotes: 1

Views: 1047

Answers (1)

Andrew Marshall
Andrew Marshall

Reputation: 96914

You're calling user_lugar_path without an ID/object, so it doesn't know which individual resource to route to. Since the resource is nested you must pass it the lugar itself and the user it is nested under:

link_to "Editar", user_lugar_path(@user, lugar)

You can also simply pass link_to the objects:

link_to "Editar", [@user, lugar]

You can read more about this in Rails Guides on routing.

Upvotes: 4

Related Questions