2012-07-18 114 views
0

對我的應用程序我有這個 我試圖創建一輛以前註冊的用戶 但我得到了錯誤(tittle post) 這是我carcontroller沒有路線匹配[POST]「/ user/1/cars」

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

    def create 
     @user = User.find(params[:user_id]) 
     @car = @user.car.create(params[:car]) 
     redirect_to user_path(@user) 
    end 
end 

這是我route.rb

Estaciones::Application.routes.draw do 
devise_for :users 

root :to => "user#index" 
resources :user do 
    resources :cars 
end 

get "user/new" 
post "user/create" 
get "user/:id" => "User#show" 
get "user/:user_id/car/new" 

,這是我html.erb的一部分

<div class="container"> 

    <h1>new user registered</h1> 

    <p> 
    <strong>name:</strong> 
    <%= @user.name %> 
    </p> 

    <p> 
    <strong>email:</strong> 
    <%= @user.email %> 
    </p> 

    <h2>new car registration</h2> 

    <%= form_for([@user, @user.car.build]) do |f| %> 
    <p> 
     <%= f.label :brand %><br /> 
     <%= f.text_field :brand %> 
    </p> 
    <p> 
     <%= f.label :color %><br /> 
     <%= f.text_field :color %> 
    </p> 
    <p> 
     <%= f.label :model %><br /> 
     <%= f.text_field :model %> 
    </p> 
    <p> 
     <%= f.label :year %><br /> 
     <%= f.text_field :year %> 
    </p> 
    <p> 
     <%= f.submit "Create new car"%> 
    </p> 
    <% end %> 
</div> 

當我提交新車的創作我得到了一個錯誤

No route matches [POST] "/user/1/cars" 

什麼想法?

也是我的路線:

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 
       root  /         user#index 
      user_cars GET /user/:user_id/cars(.:format)   cars#index 
        POST /user/:user_id/cars(.:format)   cars#create 
     new_user_car GET /user/:user_id/cars/new(.:format)  cars#new 
     edit_user_car GET /user/:user_id/cars/:id/edit(.:format) cars#edit 
      user_car GET /user/:user_id/cars/:id(.:format)  cars#show 
        PUT /user/:user_id/cars/:id(.:format)  cars#update 
        DELETE /user/:user_id/cars/:id(.:format)  cars#destroy 
      user_index GET /user(.:format)      user#index 
        POST /user(.:format)      user#create 
      new_user GET /user/new(.:format)     user#new 
      edit_user GET /user/:id/edit(.:format)    user#edit 
       user GET /user/:id(.:format)     user#show 
        PUT /user/:id(.:format)     user#update 
        DELETE /user/:id(.:format)     user#destroy 
      user_new GET /user/new(.:format)     user#new 
     user_create POST /user/create(.:format)     user#create 
        GET /user/:id(.:format)     User#show 
        GET /user/:user_id/cars/new(.:format)  car#new 
+0

任何人都可以幫我 – Asantoya 2012-07-18 20:56:57

回答

0

我不是在軌道上的人紅寶石。但是...... 您的帖子網址/users/1/cars與您在route.rb文件中提及的路線不符。

+0

我該如何解決它? – Asantoya 2012-07-18 20:45:21

+0

我該如何解決它,請任何人幫助我 – Asantoya 2012-07-18 22:15:05

0

試着這樣做:

root :to => "user#index" 
resources :users do 
    resources :cars 
end 

而是這樣的:

root :to => "user#index" 
resources :user do 
    resources :cars 
end 

(加s到用戶)。並嘗試去這裏:/用戶/ 1 /汽車

0

問題是因爲resources :user是單數,但路線想複數。路線應該是:

resources :users do 
    resources :cars 
end