路由错误没有路由匹配

时间:2012-07-24 20:34:32

标签: ruby-on-rails routing controller

我正在尝试从他的个人资料(user_path)中获取用户可以注册他的车辆(new_user_car_path)。但我得到了这个错误:

Routing Error
No route matches {:action=>"new", :controller=>"cars"}

为此我有下一个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, :only => [:new, :create, :edit, :destroy]
end

这是user_path的一部分

<div class="container">

  <fieldset>
    <h1><%= @user.email %></h1>
    <br>

    <h2>options</h2>
    <p>
    <strong>new car registration</strong>
    <%= link_to "Sign up now!", new_user_car_path, :class => "btn btn-primary" %>

    </p>
    <p>
    <strong>all cars view</strong>

    </p>
  </fieldset>
</div> <!-- /container -->

和我的CarsController

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

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

1 个答案:

答案 0 :(得分:0)

您需要将信息传递给路线,以便确定去哪里。

如果您想为用户创建新车,则需要传递用户。否则铁路将无法知道从哪里获取用户...如果你考虑它超过一秒钟,这是合乎逻辑的......

new_user_car_path(user)