0

我正在尝试从他的个人资料(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
4

1 回答 1

0

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

如果要为用户创建新车,则需要传递用户。否则,rails 将不知道从哪里获取用户...如果您考虑一秒钟以上,这是合乎逻辑的...

new_user_car_path(user)
于 2012-07-24T20:54:09.233 回答