Asantoya17
Asantoya17

Reputation: 4725

Routing Error No route matches

I'm sorry to bother again, but these routes drive me crazy, on my app later that the user create the new car it must show the car created when the user push "new car".

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

    <%= form_for ([@user,@car]) 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 %>
    <br />
    <%= link_to "Back", current_user,:class => "btn btn-primary"  %>
  </div>

ok my Carscontroller is this:

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

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

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

def index
    @car=Car.all
    respond_to do |format|
    format.html  #index.html.erb
    format.json  { render :json => @car }
  end
 end
end

so, when the user push new car it shows

Routing Error

No route matches {:action=>"show", :controller=>"cars"}
Try running rake routes for more information on available routes.

instead of showing the new car

here is 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

Upvotes: 0

Views: 635

Answers (3)

nickaknudson
nickaknudson

Reputation: 4807

Try:

redirect_to user_car_path(@user, @car)

http://guides.rubyonrails.org/routing.html#nested-resources

Upvotes: 1

uday
uday

Reputation: 8710

Your show action should be something like this:

 def show
    @user = User.find(params[:user_id])     
    @car = @user.cars.find(params[:id]) 
    redirect_to user_car_path(@user, @car)  
 end

Let me know if this doesnt work.

EDIT: There is an error in your routes.rb, its missing additional keyword end. It will be solved if you do that

Upvotes: 1

user1096557
user1096557

Reputation:

Can you post your config/routes.rb file? I would almost think you don't have the nested routes setup in there correctly, but seeing the file would help.

Upvotes: 0

Related Questions