在我的应用程序上我有这个 我正在尝试创建一个以前注册用户的汽车 但是我得到了错误(tittle post) 这是我的汽车控制器
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 :(得分:0)
我不是铁杆人的红宝石。但....
您的帖子网址/users/1/cars
与您在route.rb
文件中提及的任何路由都不匹配。
答案 1 :(得分:0)
尝试这样做:
root :to => "user#index"
resources :users do
resources :cars
end
取而代之的是:
root :to => "user#index"
resources :user do
resources :cars
end
(向用户添加s)。并尝试去这里:/ users / 1 / cars
答案 2 :(得分:0)
问题是因为resources :user
是单数,但路线想要复数。路线应该是:
resources :users do
resources :cars
end