我剛剛從我的應用程序得到這個錯誤,但我不知道它爲什麼會發生。此外,我的應用程序去一個非指定的路線(抱歉,如果它是很多代碼)。這是我的routes.rb
:未知的行動找不到行動'索引'CarController
Estaciones::Application.routes.draw do
devise_for :users
root :to => "user#index"
resources :user do
resources :car
end
get "user/new"
post "user/create"
get "user/:id" => "User#show"
end
這裏是我的用戶控制器(我沒有問題,這裏僅供參考):
Class UserController < ApplicationController
def new
@user = User.new
end
def create
@user = User.new(params[:user])
if @car.save
redirect_to :action => :show, :id => @user.id
else
redirect_to new_user_path
end
end
def show
@user = User.find(params[:id])
end
end
...我的車控制器:
class CarController < ApplicationController
def new
@car = Car.new
end
def create
@user = User.find(params[:user_id])
@car = @user.car.create(params[:car])
if @car.save
redirect_to :action => :show, :id => @user.id
else
redirect_to user_path(@user)
end
end
end
這是我的一部分html.erb
:
<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>
我的索引僅僅是一個測試,但它有這個
<h1>WELCOME TO TANKING CONTROL ONLINE</h1>
<p>
<strong>for new users </strong>
<%= link_to 'sign up', :action => :new %>
</p>
和用戶註冊的形式,它這個
<%= form_for :user, :url => { :action => :create } do |f| %>
<p>
<%= f.label :name %>
<%= f.text_field :name %>
</p>
<p>
<%= f.label :email %>
<%= f.text_field :email %>
</p>
<p>
<%= f.label :password %>
<%= f.password_field :password %>
</p>
<p>
<%= f.submit %>
</p>
<br>
<%= link_to '<<Back', :action => :index %>
<% end %>
索引模板在哪裏? – Zabba 2012-07-19 16:25:13
devise_for:用戶將照顧創建的用戶,因此不需要底層的用戶路由。 – 2012-07-19 16:28:58
我忘了我不知道爲什麼有時我也會得到這個錯誤 沒有路線匹配[POST]「/ users/1/cars」 – BlackSan 2012-07-19 16:34:07