路由错误没有路由匹配 {:action=>"show"

Asa*_*a17 3 routing routes ruby-on-rails actioncontroller

我只有 2 周的时间学习 ruby​​ on rails,在我的应用程序中,用户可以注册他们的汽车,从他们的个人资料(下面的代码)应用程序发送到注册汽车页面,

<div class="container">
        <fieldset>
            <h1><%= @user.email %></h1>
             <br> 
             <h2>now you are able to...</h2>
             <br>
             <ul>
                <li>
                    <strong>new car registration: </strong>
                    <%= link_to "new car", new_user_car_path(current_user)%>
                </li>
            </ul>
        </fieldset>
    </div>
Run Code Online (Sandbox Code Playgroud)

它以前有效,但我不知道我做了什么,现在它显示了:

Routing Error

No route matches {:action=>"show", :user_id=>#<User id: 27, email: "armando.santoya@hotmail.com", encrypted_password: "$2a$10$EZtvPWiXgMfUlAqvuvGAzODMaas/y4rGkJPKJtg4PnC6...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2012-07-24 19:07:54", last_sign_in_at: "2012-07-24 19:07:54", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", name: nil, created_at: "2012-07-24 19:07:54", updated_at: "2012-07-24 19:07:54">, :controller=>"cars"}

Try running rake routes for more information on available routes.
Run Code Online (Sandbox Code Playgroud)

我也把我的汽车控制器

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

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

  def index
    @car=Car.all
  end

  def show
   @car = current_user.car.find(params[:id])
   #@car = Car.find(params[:id])
   #redirect_to @user
  end
end
Run Code Online (Sandbox Code Playgroud)

还有我的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
Run Code Online (Sandbox Code Playgroud)

我的佣金路线:

root        /                                       static_pages#home
             contact        /contact(.:format)                      static_pages#contact
               about        /about(.:format)                        static_pages#about
    new_user_session GET    /users/sign_in(.:format)                devise/sessions#new
        user_session POST   /users/sign_in(.:format)                 devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format)               devise/sessions#destroy
       user_password POST   /users/password(.:format)               devise/passwords#create
   new_user_password GET    /users/password/new(.:format)           devise/passwords#new
  edit_user_password GET    /users/password/edit(.:format)          devise/passwords#edit
                     PUT    /users/password(.:format)               devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)                 devise/registrations#cancel
   user_registration POST   /users(.:format)                        devise/registrations#create
 new_user_registration GET    /users/sign_up(.:format)                devise/registrations#new
edit_user_registration GET    /users/edit(.:format)                   devise/registrations#edit
                     PUT    /users(.:format)                        devise/registrations#update
                     DELETE /users(.:format)                        devise/registrations#destroy
           user_cars GET    /users/:user_id/cars(.:format)          cars#index
                     POST   /users/:user_id/cars(.:format)          cars#create
        new_user_car GET    /users/:user_id/cars/new(.:format)      cars#new
       edit_user_car GET    /users/:user_id/cars/:id/edit(.:format) cars#edit
            user_car GET    /users/:user_id/cars/:id(.:format)      cars#show
                     PUT    /users/:user_id/cars/:id(.:format)      cars#update
                     DELETE /users/:user_id/cars/:id(.:format)      cars#destroy
               users GET    /users(.:format)                        users#index
                     POST   /users(.:format)                        users#create
            new_user GET    /users/new(.:format)                    users#new
           edit_user GET    /users/:id/edit(.:format)               users#edit
                user GET    /users/:id(.:format)                    users#show
                     PUT    /users/:id(.:format)                    users#update
                     DELETE /users/:id(.:format)                    users#destroy
Run Code Online (Sandbox Code Playgroud)

这是新车的 new.html.erb

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

<%= form_for(:users, :url => user_car_path(current_user)) 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 %>
Run Code Online (Sandbox Code Playgroud)

ben*_*nto 5

<%= link_to "new car", new_user_car_path(current_user)%>
Run Code Online (Sandbox Code Playgroud)

考虑到你的路线应该没问题

     new_user_car GET    /users/:user_id/cars/new(.:format)      cars#new
Run Code Online (Sandbox Code Playgroud)

但是错误说缺少 car#show(不是新的!),所以也许会寻找它。

什么时候准确抛出错误?

ADDITION: 现在你已经发布了表格,

我认为产生错误的行是

<%= form_for(:users, :url => user_car_path(current_user)) do |f| %>
Run Code Online (Sandbox Code Playgroud)

因为 user_car_path 需要用户和汽车 - 所以你需要

user_car_path(current_user,@car)
Run Code Online (Sandbox Code Playgroud)

我在我的表单中只使用了这个:

<%= form_for ([@user,@car]) do |f| %>
Run Code Online (Sandbox Code Playgroud)

但最重要的是,每次您引用汽车时,您还需要包含用户引用。