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
You’re calling
user_lugar_pathwithout an ID/object, so it doesn’t know which individual resource to route to. Since the resource is nested you must pass it thelugaritself and theuserit is nested under:You can also simply pass
link_tothe objects:You can read more about this in Rails Guides on routing.