Question

I only have 2 weeks learning ruby on rails, in my app the users can register their cars, from their profile (code below) the app sent to the registration cars page,

<div class="container">
        <fieldset>
            <h1><%= @user.email %></h1>
             <br> 
             <h2>now you are able to...</h2>
             <br>
             <ul>
                <li>
                    <strong>new car registration: </strong>
                    <%= link_to "new car", new_user_car_path(current_user)%>
                </li>
            </ul>
        </fieldset>
    </div>

it works before but i don't know what i did that now it show this:

Routing Error

No route matches {:action=>"show", :user_id=>#<User id: 27, email: "armando.santoya@hotmail.com", encrypted_password: "$2a$10$EZtvPWiXgMfUlAqvuvGAzODMaas/y4rGkJPKJtg4PnC6...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2012-07-24 19:07:54", last_sign_in_at: "2012-07-24 19:07:54", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", name: nil, created_at: "2012-07-24 19:07:54", updated_at: "2012-07-24 19:07:54">, :controller=>"cars"}

Try running rake routes for more information on available routes.

also I put my carsController

class CarsController < ApplicationController
  def new
    @car = Car.new
  end

  def create
    @car = current_user.Car.new(params[:car])
    if @car.save
      flash[:notice] = "new car created success"
      #redirect_to current_user, :flash => { :success => "car created!" }
    else
      #redirect_to new_user_car_path, 
      flash[:notice] = "sorry try again"
    end
  end

  def index
    @car=Car.all
  end

  def show
   @car = current_user.car.find(params[:id])
   #@car = Car.find(params[:id])
   #redirect_to @user
  end
end

and my routes.rb

Estaciones::Application.routes.draw do
root :to => "static_pages#home"
match '/contact', :to=>'static_pages#contact'
match '/about', :to=>'static_pages#about'
devise_for :users
resources :users do
resources :cars
end

my rake routes:

root        /                                       static_pages#home
             contact        /contact(.:format)                      static_pages#contact
               about        /about(.:format)                        static_pages#about
    new_user_session GET    /users/sign_in(.:format)                devise/sessions#new
        user_session POST   /users/sign_in(.:format)                 devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format)               devise/sessions#destroy
       user_password POST   /users/password(.:format)               devise/passwords#create
   new_user_password GET    /users/password/new(.:format)           devise/passwords#new
  edit_user_password GET    /users/password/edit(.:format)          devise/passwords#edit
                     PUT    /users/password(.:format)               devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)                 devise/registrations#cancel
   user_registration POST   /users(.:format)                        devise/registrations#create
 new_user_registration GET    /users/sign_up(.:format)                devise/registrations#new
edit_user_registration GET    /users/edit(.:format)                   devise/registrations#edit
                     PUT    /users(.:format)                        devise/registrations#update
                     DELETE /users(.:format)                        devise/registrations#destroy
           user_cars GET    /users/:user_id/cars(.:format)          cars#index
                     POST   /users/:user_id/cars(.:format)          cars#create
        new_user_car GET    /users/:user_id/cars/new(.:format)      cars#new
       edit_user_car GET    /users/:user_id/cars/:id/edit(.:format) cars#edit
            user_car GET    /users/:user_id/cars/:id(.:format)      cars#show
                     PUT    /users/:user_id/cars/:id(.:format)      cars#update
                     DELETE /users/:user_id/cars/:id(.:format)      cars#destroy
               users GET    /users(.:format)                        users#index
                     POST   /users(.:format)                        users#create
            new_user GET    /users/new(.:format)                    users#new
           edit_user GET    /users/:id/edit(.:format)               users#edit
                user GET    /users/:id(.:format)                    users#show
                     PUT    /users/:id(.:format)                    users#update
                     DELETE /users/:id(.:format)                    users#destroy

this is the new.html.erb for new cars

<div class="container">
<h2>new car registration</h2>

<%= form_for(:users, :url => user_car_path(current_user)) do |f| %>
  <div><%= f.label :brand %><br />
    <%= f.text_field :brand %></div>

    <div><%= f.label :color %><br />
    <%= f.text_field :color %></div>

    <div><%= f.label :model %><br />
    <%= f.text_field :model %></div>

  <div><%= f.label :year %><br />
  <%= f.text_field :year %></div>

    <div><%= f.submit "new car",:class => "btn btn-primary" %></div>
<% end %>

Was it helpful?

Solution

<%= link_to "new car", new_user_car_path(current_user)%>

should be ok given your route

     new_user_car GET    /users/:user_id/cars/new(.:format)      cars#new

but the error says that a car#show (not new!) is missing, so maybe look for that.

When is the error exactly thrown?

ADDITION: now that you've posted the form,

I think the line producing the error is

<%= form_for(:users, :url => user_car_path(current_user)) do |f| %>

because user_car_path needs both a user and a car - so you would need

user_car_path(current_user,@car)

I've used just this in my form:

<%= form_for ([@user,@car]) do |f| %>

but the bottom line is, every time you're referencing a car, you need also the enclosing user reference.

OTHER TIPS

I don't know if it is generating the problem, but why are you passin the current_user here:

<%= link_to "new car", new_user_car_path(current_user)%>

In your creation action, you are already getting the current user:

@car = current_user.Car.new(params[:car])

Also in your show action you have car and not Car

def show
  @car = current_user.car.find(params[:id])

EDIT - based in your routes, I can see you have a nested resource:

your cars controller should be:

class CarsController < ApplicationController
def new
  @user = User.find(params[:user_id])
  @car = @user.cars.build
end

def create
  @user = User.find(params[:user_id])
  @car = @user.cars.build(params[:car])
  if @car.save
    flash[:notice] = "new car created success"
    #redirect_to current_user, :flash => { :success => "car created!" }
  else
    #redirect_to new_user_car_path, 
    flash[:notice] = "sorry try again"
  end
end

def index
  @car=Car.all
end
...

And the link to create the new car is new_user_car_path

For what it's worth, I had a problem very similar to the one described by OP: the "index" action for my controller worked, but the "new" action threw the same exception ("show" route missing, which was not the case). It turned out Rails had some problem with the pluralization of my model name (ending in a 'y').

Rather than fighting the convention, I ended up choosing another model name with simpler pluralization and things were fine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top