0

在我的应用程序上我有这个我正在尝试创建以前注册用户的汽车但我收到错误(标题帖子)这是我的汽车控制器

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

这是我的路线.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
4

3 回答 3

0

我不是 Ruby on Rails 人。但是....您的帖子网址与您在文件/users/1/cars中提到的任何路线都不匹配。route.rb

于 2012-07-18T20:42:28.540 回答
0

尝试这样做:

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

取而代之的是:

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

(添加到用户)。并尝试去这里:/users/1/cars

于 2012-07-18T22:20:50.300 回答
0

问题是因为resources :user是单数,但路线需要复数。路线应该是:

resources :users do
  resources :cars
end
于 2012-07-18T22:26:39.040 回答