BlackSan
BlackSan

Reputation: 58

Unknown action The action 'index' could not be found for CarController

I just get this error from my app but I don't know why it's happening. Also, my app goes to a non-specified route (sorry if it is a lot of code). This is my routes.rb:

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

Here is my user controller (I don't have problem here is only for refer):

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

...my car controller:

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

and this is part of my 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>

my index is only a test but it has this

<h1>WELCOME TO TANKING CONTROL ONLINE</h1>
<p>
  <strong>for new users </strong>
  <%= link_to 'sign up', :action => :new %>
</p>

and the form of user registration it this

<%= 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 %>

Upvotes: 0

Views: 3406

Answers (1)

Carson Cole
Carson Cole

Reputation: 4451

You are missing an action 'index' in your CarController..

def index
end

Make sure you have an 'index.html.erb' in your 'cars' views.

[UPDATE] Your routes should be

resources :users do
  resources :cars
end

Notice the plurality of users and cars.

Upvotes: 2

Related Questions