路由错误没有路由匹配
问题描述:
我试图让用户从他的个人资料(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
答
您需要将信息传递给路由,以便它可以确定去哪里。
如果要为用户创建新车,则需要传递用户。否则轨道将不知道从哪里得到用户......如果您想了解更多的一秒钟,这将是合乎逻辑的...
new_user_car_path(user)
wou我有太多了解轨道 – Asantoya17 2012-07-24 20:56:02