Routing Error No route matches {:action=>"show" - ruby-on-rails

I only have 2 weeks learning ruby on rails, in my app the users can register their cars, from their profile (code below) the app sent to the registration cars page,
<div class="container">
<fieldset>
<h1><%= #user.email %></h1>
<br>
<h2>now you are able to...</h2>
<br>
<ul>
<li>
<strong>new car registration: </strong>
<%= link_to "new car", new_user_car_path(current_user)%>
</li>
</ul>
</fieldset>
</div>
it works before but i don't know what i did that now it show this:
Routing Error
No route matches {:action=>"show", :user_id=>#<User id: 27, email: "armando.santoya#hotmail.com", encrypted_password: "$2a$10$EZtvPWiXgMfUlAqvuvGAzODMaas/y4rGkJPKJtg4PnC6...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2012-07-24 19:07:54", last_sign_in_at: "2012-07-24 19:07:54", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", name: nil, created_at: "2012-07-24 19:07:54", updated_at: "2012-07-24 19:07:54">, :controller=>"cars"}
Try running rake routes for more information on available routes.
also I put my carsController
class CarsController < ApplicationController
def new
#car = Car.new
end
def create
#car = current_user.Car.new(params[:car])
if #car.save
flash[:notice] = "new car created success"
#redirect_to current_user, :flash => { :success => "car created!" }
else
#redirect_to new_user_car_path,
flash[:notice] = "sorry try again"
end
end
def index
#car=Car.all
end
def show
#car = current_user.car.find(params[:id])
##car = Car.find(params[:id])
#redirect_to #user
end
end
and my routes.rb
Estaciones::Application.routes.draw do
root :to => "static_pages#home"
match '/contact', :to=>'static_pages#contact'
match '/about', :to=>'static_pages#about'
devise_for :users
resources :users do
resources :cars
end
my rake routes:
root / static_pages#home
contact /contact(.:format) static_pages#contact
about /about(.:format) static_pages#about
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
user_cars GET /users/:user_id/cars(.:format) cars#index
POST /users/:user_id/cars(.:format) cars#create
new_user_car GET /users/:user_id/cars/new(.:format) cars#new
edit_user_car GET /users/:user_id/cars/:id/edit(.:format) cars#edit
user_car GET /users/:user_id/cars/:id(.:format) cars#show
PUT /users/:user_id/cars/:id(.:format) cars#update
DELETE /users/:user_id/cars/:id(.:format) cars#destroy
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
this is the new.html.erb for new cars
<div class="container">
<h2>new car registration</h2>
<%= form_for(:users, :url => user_car_path(current_user)) do |f| %>
<div><%= f.label :brand %><br />
<%= f.text_field :brand %></div>
<div><%= f.label :color %><br />
<%= f.text_field :color %></div>
<div><%= f.label :model %><br />
<%= f.text_field :model %></div>
<div><%= f.label :year %><br />
<%= f.text_field :year %></div>
<div><%= f.submit "new car",:class => "btn btn-primary" %></div>
<% end %>

<%= link_to "new car", new_user_car_path(current_user)%>
should be ok given your route
new_user_car GET /users/:user_id/cars/new(.:format) cars#new
but the error says that a car#show (not new!) is missing, so maybe look for that.
When is the error exactly thrown?
ADDITION: now that you've posted the form,
I think the line producing the error is
<%= form_for(:users, :url => user_car_path(current_user)) do |f| %>
because user_car_path needs both a user and a car - so you would need
user_car_path(current_user,#car)
I've used just this in my form:
<%= form_for ([#user,#car]) do |f| %>
but the bottom line is, every time you're referencing a car, you need also the enclosing user reference.

I don't know if it is generating the problem, but why are you passin the current_user here:
<%= link_to "new car", new_user_car_path(current_user)%>
In your creation action, you are already getting the current user:
#car = current_user.Car.new(params[:car])
Also in your show action you have car and not Car
def show
#car = current_user.car.find(params[:id])
EDIT - based in your routes, I can see you have a nested resource:
your cars controller should be:
class CarsController < ApplicationController
def new
#user = User.find(params[:user_id])
#car = #user.cars.build
end
def create
#user = User.find(params[:user_id])
#car = #user.cars.build(params[:car])
if #car.save
flash[:notice] = "new car created success"
#redirect_to current_user, :flash => { :success => "car created!" }
else
#redirect_to new_user_car_path,
flash[:notice] = "sorry try again"
end
end
def index
#car=Car.all
end
...
And the link to create the new car is new_user_car_path

For what it's worth, I had a problem very similar to the one described by OP: the "index" action for my controller worked, but the "new" action threw the same exception ("show" route missing, which was not the case). It turned out Rails had some problem with the pluralization of my model name (ending in a 'y').
Rather than fighting the convention, I ended up choosing another model name with simpler pluralization and things were fine.

Related

Two users#show, and how to redirect to my user profile after login

Happy Thursday everyone, I had a quick question on routes and redirecting. I am working on a rails assignment that asks that I redirect the router to his/her profile after signing in. How would I go about doing that? An after_sign_in method? Here is my routes:
Rails.application.routes.draw do
get 'users/show'
get 'users_controller/show'
devise_for :users
resources :users
get 'welcome/index'
root :to => 'welcome#index'
end
Users Controller:
class UsersController < ApplicationController
def show
#user = User.find(params[:id])
end
private
def user_params
params.require(:user).permit(:name, :email)
end
end
devise/sessions (Login Page)
<h2>Sign in</h2>
<div class="row">
<div class="col-md-8">
<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="form-group">
<%= f.label :email %>
<%= f.email_field :email, autofocus: true, class: 'form-control', placeholder: "Enter email" %>
</div>
<div class="form-group">
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control', placeholder: "Enter password" %>
</div>
<div class="form-group">
<% if devise_mapping.rememberable? %>
<%= f.label :remember_me, class: 'checkbox' do %>
<%= f.check_box :remember_me %> Remember me
<% end %>
<% end %>
<%= f.submit "Sign in", class: 'btn btn-success' %>
</div>
<div class="form-group">
<%= render "devise/shared/links" %>
</div>
<% end %>
</div>
</div>
Thanks for your help! Also could anyone answer why my rake routes has two users#show? I know only one functions (the one with the user_id) so I don't know how two were created.
Rake Routes Output:
rake routes
Prefix Verb URI Pattern Controller#Action
users_show GET /users/show(.:format) users#show
users_controller_show GET /users_controller/show(.:format) users_controller#show
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
PATCH /users/password(.:format) devise/passwords#update
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
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
user_confirmation POST /users/confirmation(.:format) devise/confirmations#create
new_user_confirmation GET /users/confirmation/new(.:format) devise/confirmations#new
GET /users/confirmation(.:format) devise/confirmations#show
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
welcome_index GET /welcome/index(.:format) welcome#index
root GET /
welcome#index
Define after_sign_in_path_for in ApplicationController. Read this: https://github.com/plataformatec/devise/wiki/How-To%3A-Redirect-to-a-specific-page-on-successful-sign-in-and-sign-out
By default, Devise will redirect the user to the root_path after signing in. You can simply change the root setting in config/routes.rb to the desired path, or add a user_root_path, e.g.
get '/welcome' => "welcome#index", as: :user_root
as describe in the Devise wiki.
As to the two routes, note that your routes.rb includes these two statements:
get 'users/show'
resources :users
resources automatically adds a route at GET /users/:id mapping to UsersController#show, which is what you want. The other route should be removed, along with get 'users_controller/show'. More info on this can be found in the Rails Routing Guide.
In the applicaion controller
def after_sign_in_path_for(user)
the link that you want
end
in your example
def after_sign_in_path_for(user)
user_path(current_user.id)
end

Comments to my reddit clone ruby pulls a NameError

I'm trying to add comment functionality of my Reddit clone. This is the comments controller that creates a comments and adds it to a post.
class CommentsController < ApplicationController
def new
#topic = Topic.find(params[:topic_id])
#post = Post.find(params[:id])
#comment = Comment.new
#authorize #comment # from include Pundit in the application controller, authorize is an inherited method
end
def create
#topic = Topic.find(params[:topic_id])
#post = Post.find(params[:id])
#comment = current_user.comments.build(comment_params)
end
private
def comment_params
params.require(:comment).permit(:text)
end
end
I'm trying to add a comments field for every post page by using a form partial that looks like this:
<%= form_for [topic, post] do |f| %>
<%= form_group_tag(comment[:text]) do %>
<%= f.label :text %>
<%= f.text_area :text, rows: 10, class: 'form-control', placeholder: "Enter your comment" %>
<% end %>
<div class = "form-group">
<%= f.submit "Save", class: 'btn btn-success' %>
</div>
<% end %>
This form partial should appear at the post.show.html.erb so I put it there
<h1><%= markdown #post.title %></h1>
<div class="row"> <!-- what others are there besides row? -->
<div class="col-md-8">
<p><%= markdown #post.body %></p>
</div>
<div class="col-md-4">
<% if policy(#post).edit? %>
<%= link_to "Edit", edit_topic_post_path(#topic, #post), class: 'btn btn-success' %>
<% end %>
</div>
<div class="col-md-8">
<%= render partial: 'comments/form', locals: { topic: #topic, post: #post, text: #post.comments.new } %>
</div>
</div>
but I'm getting a NameError for my 'comment' on the form_group_tag line. Most of what I defined here comes from my code for adding new posts, which seemed to work. Is there something missing here?
I fixed my name error by adding comments to the form_for line, but I'm getting NoMethodError for my topic,post,comment path, so I thought it'd be helpful to add what rake routes is pulling up.
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
PATCH /users/password(.:format) devise/passwords#update
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
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
user_confirmation POST /users/confirmation(.:format) devise/confirmations#create
new_user_confirmation GET /users/confirmation/new(.:format) devise/confirmations#new
GET /users/confirmation(.:format) devise/confirmations#show
user PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
topic_posts POST /topics/:topic_id/posts(.:format) posts#create
new_topic_post GET /topics/:topic_id/posts/new(.:format) posts#new
edit_topic_post GET /topics/:topic_id/posts/:id/edit(.:format) posts#edit
topic_post GET /topics/:topic_id/posts/:id(.:format) posts#show
PATCH /topics/:topic_id/posts/:id(.:format) posts#update
PUT /topics/:topic_id/posts/:id(.:format) posts#update
DELETE /topics/:topic_id/posts/:id(.:format) posts#destroy
topics GET /topics(.:format) topics#index
POST /topics(.:format) topics#create
new_topic GET /topics/new(.:format) topics#new
edit_topic GET /topics/:id/edit(.:format) topics#edit
topic GET /topics/:id(.:format) topics#show
PATCH /topics/:id(.:format) topics#update
PUT /topics/:id(.:format) topics#update
DELETE /topics/:id(.:format) topics#destroy
post_comments POST /posts/:post_id/comments(.:format) comments#create
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format) posts#show
PATCH /posts/:id(.:format) posts#update
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
about GET /about(.:format) welcome#about
root GET / welcome#index
BTW: how does this routes.rb file look?
Rails.application.routes.draw do
devise_for :users
resources :users, only: [:update]
resources :topics do
resources :posts, except: [:index]
end
resources :posts do
resources :comments, only: [:create]
end
end
Try:
<%= render partial: 'comments/form', locals: { topic: #topic, post: #post, comment: #post.comments.new } %>
...
<%= form_for [post, comment] do |f| %>
<%= f.label :text %>
<%= f.text_area :text, rows: 10, class: 'form-control', placeholder: "Enter your comment" %>
<%= f.submit "Save", class: 'btn btn-success' %>
<% end %>
...
If it does not help, try to temporary comment form_group_tag and send error here
It seems like you're sending the comment down to the partial as text.
... locals: { topic: #topic, post: #post, text: #post.comments.new } %>
^^^^
And by the way you're not saving the comment in the create action.

Rails no route error, while the rake routes gives the route

I am dealing with a nested resource "farm" in Rails, and my form for making new farm looks like this:
<%= form_for([#user, #farm], :url=> new_user_farm_path(#user)) do |f| %>
<% if #farm.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#farm.errors.count, "error") %> prohibited this farm from being saved:</h2>
<ul>
<% #farm.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :contact %><br />
<%= f.text_field :contact %>
</div>
<div class="field">
<%= f.label :adress %><br />
<%= f.text_field :adress %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
My corresponding "new" function in the farms controller is:
def new
#farm = Farm.new
#user = User.find(current_user)
respond_to do |format|
format.html # new.html.erb
format.json { render json: #farm }
end
end
It renders the form just fine, but after I click submit, and it actually tries to create new Farm, I get this error:
No route matches [POST] "/users/2/farm/new"
In my rake routes, I clearly have thie route showing:
user_farm POST /users/:user_id/farm(.:format) {:action=
create", :controller=>"farms"}
I am only guessing that the problem is in my create function:
def create
#farm = Farm.new(params[:farm])
#user = User.find(current_user)
respond_to do |format|
if #farm.save
format.html { redirect_to user_farm(#user, #farm), notice: 'Farm was successfully created.' }
format.json { render json: #farm, status: :created, location: #farm }
else
format.html { render action: "new" }
format.json { render json: #farm.errors, status: :unprocessable_entity }
end
end
end
My routes.rb file:
resources :users do
resource :farm
end
devise_for :users, :path => 'accounts'
I am accessing my new farm form via this link:
<%= link_to "New Farm", new_user_farm_path(current_user) %>
My entire rake routes:
user_farm POST /users/:user_id/farm(.:format) {:action=>"
create", :controller=>"farms"}
new_user_farm GET /users/:user_id/farm/new(.:format) {:action=>"
new", :controller=>"farms"}
edit_user_farm GET /users/:user_id/farm/edit(.:format) {:action=>"
edit", :controller=>"farms"}
GET /users/:user_id/farm(.:format) {:action=>"
show", :controller=>"farms"}
PUT /users/:user_id/farm(.:format) {:action=>"
update", :controller=>"farms"}
DELETE /users/:user_id/farm(.:format) {:action=>"
destroy", :controller=>"farms"}
users GET /users(.:format) {:action=>"
index", :controller=>"users"}
POST /users(.:format) {:action=>"
create", :controller=>"users"}
new_user GET /users/new(.:format) {:action=>"
new", :controller=>"users"}
edit_user GET /users/:id/edit(.:format) {:action=>"
edit", :controller=>"users"}
user GET /users/:id(.:format) {:action=>"
show", :controller=>"users"}
PUT /users/:id(.:format) {:action=>"
update", :controller=>"users"}
DELETE /users/:id(.:format) {:action=>"
destroy", :controller=>"users"}
new_user_session GET /accounts/sign_in(.:format) {:action=>"
new", :controller=>"devise/sessions"}
user_session POST /accounts/sign_in(.:format) {:action=>"
create", :controller=>"devise/sessions"}
destroy_user_session GET /accounts/sign_out(.:format) {:action=>"
destroy", :controller=>"devise/sessions"}
user_password POST /accounts/password(.:format) {:action=>"
create", :controller=>"devise/passwords"}
new_user_password GET /accounts/password/new(.:format) {:action=>"
new", :controller=>"devise/passwords"}
edit_user_password GET /accounts/password/edit(.:format) {:action=>"
edit", :controller=>"devise/passwords"}
PUT /accounts/password(.:format) {:action=>"
update", :controller=>"devise/passwords"}
cancel_user_registration GET /accounts/cancel(.:format) {:action=>"
cancel", :controller=>"devise/registrations"}
user_registration POST /accounts(.:format) {:action=>"
create", :controller=>"devise/registrations"}
new_user_registration GET /accounts/sign_up(.:format) {:action=>"
new", :controller=>"devise/registrations"}
edit_user_registration GET /accounts/edit(.:format) {:action=>"
edit", :controller=>"devise/registrations"}
PUT /accounts(.:format) {:action=>"
update", :controller=>"devise/registrations"}
DELETE /accounts(.:format) {:action=>"
destroy", :controller=>"devise/registrations"}
user_confirmation POST /accounts/confirmation(.:format) {:action=>"
create", :controller=>"devise/confirmations"}
new_user_confirmation GET /accounts/confirmation/new(.:format) {:action=>"
new", :controller=>"devise/confirmations"}
GET /accounts/confirmation(.:format) {:action=>"
show", :controller=>"devise/confirmations"}
user_unlock POST /accounts/unlock(.:format) {:action=>"
create", :controller=>"devise/unlocks"}
new_user_unlock GET /accounts/unlock/new(.:format) {:action=>"
new", :controller=>"devise/unlocks"}
GET /accounts/unlock(.:format) {:action=>"
show", :controller=>"devise/unlocks"}
home_index GET /home/index(.:format) {:controlle
r=>"home", :action=>"index"}
root / {:controlle
r=>"home", :action=>"index"}
The problem is that your form is attempting to make a POST request to a url that only exists for GET requests. So it's telling you that
[POST] "/users/2/farm/new"
doesn't exist -- which it doesn't. And your rake routes output confirms this -- the cloesst thing is
new_user_farm GET /users/:user_id/farm/new(.:format)
which is a GET request.
Forms default to using POST for new record creation, so you need to supply a url that can be POSTed to. Or you can let Rails figure it out from the state of your objects. So either
<%= form_for([#user, #farm], :url=> user_farm_path(#user)) do |f| %>
OR
<%= form_for([#user, #farm]) do |f| %>
should work. In the former case we're using a named route that matches a POST route from your rake routes output. In the latter case we're letting rails figure it out based on the state of your #farm object. That is, if #farm is a new_record? it'll submit a POST request to /users/:user_id/farm, or if #farm is persisted? then it'll submit a PUT request to /users/:user_id/farm. (Same path, just different request type.)
Some issues:
--
Params
When you create a new ActiveRecord object in Rails, you'll want to use strong params:
#app/controllers/farms_controller.rb
Class FarmsController < ApplicationController
def new
#farm = Farm.new
end
def create
#farm = Farm.new(farm_params)
end
private
def farm_params
params.require(:farm).permit(:params, :for, :farm)
end
end
--
Routes
You're using nested routing, which can be tricky if you're not used to it.
If you haven't already, you should do this with your routes:
#config/routes.rb
resources :users do
resources :farm #-> /users/3/farm/new
end
This will give you the ability to reach that route either from your controller or views.
If you give me some more info (Routes file), I'll be able to help further!
--
POST
Having looked over the issue again, it seems I made an oversight before!!!
As pointed out by pdbobb, the error certainly says you're trying to reach /new with a POST verb. This is not correct, as according to the Rails resourceful routing conventions, you need to
You'll be able to use pdobb's answer, but more importantly, we need to establish why your form is trying to submit to the new post.
The problem is likely with your nested resource

No route matches [POST]

on my app i have this
i'm trying to create a car of a previously registered user
but i got the error (tittle post)
this is my carcontroller
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
this is my 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"
and this is part of my 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>
when i submit the creation of the new car i got the next error
No route matches [POST] "/user/1/cars"
any idea??
also my routes:
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
You need CarsController not CarController

No route matches [POST] "/user/1/cars"

on my app i have this
i'm trying to create a car of a previously registered user
but i got the error (tittle post)
this is my carcontroller
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
this is my 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"
and this is part of my 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>
when i submit the creation of the new car i got the next error
No route matches [POST] "/user/1/cars"
any idea??
also my routes:
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
I am not a ruby on rails person. But....
your post url /users/1/cars does not match any of the routes you mentioned in your route.rb file.
Try to do this :
root :to => "user#index"
resources :users do
resources :cars
end
Instead this :
root :to => "user#index"
resources :user do
resources :cars
end
(add a s to user). And try to go here : /users/1/cars
The problem is because resources :user is singular, but the route wants a plural. The routes should be:
resources :users do
resources :cars
end

Resources