2012-07-19 42 views
0

我剛剛從我的應用程序得到這個錯誤,但我不知道它爲什麼會發生。此外,我的應用程序去一個非指定的路線(抱歉,如果它是很多代碼)。這是我的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 %> 
+0

索引模板在哪裏? – Zabba 2012-07-19 16:25:13

+0

devise_for:用戶將照顧創建的用戶,因此不需要底層的用戶路由。 – 2012-07-19 16:28:58

+0

我忘了我不知道爲什麼有時我也會得到這個錯誤 沒有路線匹配[POST]「/ users/1/cars」 – BlackSan 2012-07-19 16:34:07

回答

2

你在你CarController缺少行動「指數」 ..

def index 
end 

確保您的'汽車'視圖中有'index.html.erb'。

[更新]你的路線應該是

resources :users do 
    resources :cars 
end 

通知多個用戶和汽車。

+0

我做了,但現在我得到一個新的錯誤...我必須打開一個最新帖子? – BlackSan 2012-07-19 16:38:52

+0

什麼是錯誤? – 2012-07-19 16:42:30

+0

@BlackSan答覆已更新與您的帖子問題。 – 2012-07-19 16:54:57