【问题标题】:Routing Error No route matches路由错误没有路由匹配
【发布时间】:2012-07-24 20:34:32
【问题描述】:

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

这里是用户路径的一部分

<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

【问题讨论】:

    标签: ruby-on-rails routing controller


    【解决方案1】:

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

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

    new_user_car_path(user)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多