This question is admittedly long. So I appreciate any support from the rubytutorial community. I am in Chapter 9, attempting to create a session for a logged-in user.
I've done the tutorial already in < Rails 3.1. Since I am now using Rails 3.1, I headed on over to Chapter 13 and linked to the (very good) Railscasts (#270) on the subject. I was able to rewrite my user sign up pretty easily thanks to has_secure_password.
When I try to log in with a user in the database I see this in the console):
No route matches {:action=>"show", :controller=>"users"}
Seems like I need to create a route and it should work. But if that is the case, why can I go 'users/1' and the view appear? I am using the route user_path(#user), #user in my sessions and users controllers (below).
Here is what I did.
Pass a form to the Session controller new action (note: I use form_tag and not form_for)
<%= form_tag sessions_path do %>
<div class="field">
<%= label_tag :email %><br />
<%= text_field_tag :email, params[:email] %>
</div>
<div class="field">
<%= label_tag :password %><br />
<%= password_field_tag :password %>
</div>
<div class="actions">
<%= submit_tag "Sign In" %>
</div>
<% end %>
Then, create action in sessions_controller.rb
def create
#Assign object by email attribute
user = User.find_by_email(params[:email])
# User is present and has access, must be true otherwise nil object
if user && user.authenticate(params[:password])
session[:user_id] = user.id
RIGHT HERE IS THE PROBLEM
redirect_to user_path(#user), :notice => "Logged in!"
else
#Use flash.now on render not flash[]
flash.now.alert = "Invalid email or password"
render "new"
end
end
And finally Create action for users_controller.rb, which works fine.
def create
#user = User.new(params[:user])
if #user.save
flash[:success] = "Welcome to the Sample App!"
redirect_to #user
else
render "new"
#user.password.clear
#user.password_confirmation.clear
end
end
Leaving my User model:
attr_accessible :name, :email, :password, :password_confirmation
email_regex = /\A[\w+\-.]+#[a-z\d\-.]+\.[a-z]+\z/i
has_secure_password
Oh and here is my current routes.rb file
resources :users
resources :sessions, only: [:create, :new, :destroy]
root to: "pages#home"
match "/about", to: "pages#about"
match "/contact", to: "pages#contact"
match "/help", to: "pages#help"
match "/signup", to: "users#new"
match "/signin", to: "sessions#new"
match "/signout", to: "sessions#destroy"
And finally my output when I run rake routes:
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"}
sessions POST /sessions(.:format) {:action=>"create", :controller=>"sessions"}
new_session GET /sessions/new(.:format) {:action=>"new", :controller=>"sessions"}
session DELETE /sessions/:id(.:format) {:action=>"destroy", :controller=>"sessions"}
root / {:controller=>"pages", :action=>"home"}
about /about(.:format) {:controller=>"pages", :action=>"about"}
contact /contact(.:format) {:controller=>"pages", :action=>"contact"}
help /help(.:format) {:controller=>"pages", :action=>"help"}
signup /signup(.:format) {:controller=>"users", :action=>"new"}
signin /signin(.:format) {:controller=>"sessions", :action=>"new"}
signout /signout(.:format) {:controller=>"sessions", :action=>"destroy"}
It's your redirect_to #user and/or user_path(#user) lines. They generate requests to /users/:id, which apparently isn't in your routes.rb.
#user is nil in your example - you're assigning to user. What rails is telling you in a not very helpful way is that it can't route to :controller => 'users', :action => 'show' without a user.
Related
I am following along this tutorial's authentication process - currently on the section 'User Authentication':
http://larsgebhardt.de/user-authentication-with-ruby-on-rails-rspec-and-capybara/
I received the follower test failure:
1) User Management User log in
Failure/Error: login(#writer)
ActionController::RoutingError:
uninitialized constant SessionController
# ./spec/support/user_helper.rb:6:in `login'
# ./spec/features/users_spec.rb:27:in `block (2 levels) in <top (required)>'
Here is the specific test and helper file mention in the failure message, along with other files..
spec/features/users_spec.rb
require 'spec_helper'
background do
#writer = create(:user, :writer)
end
....
scenario 'User log in' do
activate(#writer)
login(#writer)
expect(page).to have_content "Successfully logged in."
end
spec/support/user_helper.rb
module UserHelper
def login(a)
visit root_path
click_link 'Log In'
fill_in 'session[email]', with: a.email
fill_in 'session[password]', with: a.password
click_button 'Log In'
end
def logout(a)
visit root_path
click_link 'Log Out'
end
def activate(a)
visit activate_path(:code => a.activation_code)
end
end
routes.rb
resources :session
sessions_controller.rb
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by_email(params[:session][:email]).try(:authenticate, params[:session][:password])
if user
if user.is_active?
session[:user_id] = user.id
redirect_to (session[:target_url] || root_path)
flash[:notice] = "Successfully logged in."
else
redirect_to new_session_path
flash[:error] = "Account inactive. Please activate your account."
end
else
redirect_to new_session_path
flash[:error] = "Invalid email or password."
end
end
def destroy
session[:user_id] = nil
redirect_to root_path
flash[:notice] = "Successfully logged out."
end
end
new.html.erb
<h1>Log In</h1>
<%= form_for :session, url: sessions_path do |f| %>
<div>
<%= f.label :email %>
<%= f.text_field :email %>
</div>
<div>
<%= f.label :password %>
<%= f.password_field :password %>
</div>
<div>
<%= f.submit 'Log In' %>
</div>
<% end %>
rake routes
Prefix Verb URI Pattern Controller#Action
profiles_show GET /profiles/show(.:format) profiles#show
sessions_new GET /sessions/new(.:format) sessions#new
users_new GET /users/new(.:format) users#new
root GET / sessions#new
post_comments GET /posts/:post_id/comments(.:format) comments#index
POST /posts/:post_id/comments(.:format) comments#create
new_post_comment GET /posts/:post_id/comments/new(.:format) comments#new
edit_post_comment GET /posts/:post_id/comments/:id/edit(.:format) comments#edit
post_comment GET /posts/:post_id/comments/:id(.:format) comments#show
PATCH /posts/:post_id/comments/:id(.:format) comments#update
PUT /posts/:post_id/comments/:id(.:format) comments#update
DELETE /posts/:post_id/comments/:id(.:format) comments#destroy
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
sessions GET /sessions(.:format) sessions#index
POST /sessions(.:format) sessions#create
new_session GET /sessions/new(.:format) sessions#new
edit_session GET /sessions/:id/edit(.:format) sessions#edit
session GET /sessions/:id(.:format) sessions#show
PATCH /sessions/:id(.:format) sessions#update
PUT /sessions/:id(.:format) sessions#update
DELETE /sessions/:id(.:format) sessions#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
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
profile_index GET /profile(.:format) profile#index
POST /profile(.:format) profile#create
new_profile GET /profile/new(.:format) profile#new
edit_profile GET /profile/:id/edit(.:format) profile#edit
profile GET /profile/:id(.:format) profile#show
PATCH /profile/:id(.:format) profile#update
PUT /profile/:id(.:format) profile#update
DELETE /profile/:id(.:format) profile#destroy
activate GET /activate/:code(.:format) users#activate
_header.html.erb
About
Services
<%= link_to "Sign Up", new_user_path %>
<%= link_to "Log In", new_session_path %>
<%= link_to "Log Out", "/session", method: :delete %>
I previously had an issue with configuring the User Helper file which was solved but incase it becomes relevant, you can see the answer and state of the config file here:
Rspec email_spec issue
You need resources :sessions, you are missing the 's'.
Edit :
You are using the delete route incorrectly, you should say
<%= link_to "Log Out",session_path(pass the current signed in user here) , method: :delete %>
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
When I submit this form
<h1>Log In</h1>
<%= simple_form_for sessions_path do |f| %>
<%= f.input :email %>
<%= f.input :password %>
<%= f.input :remember_me, as: :boolean %>
<%= f.submit 'Log In' %>
<% end %>
It gives me this error: No route matches [POST] "/login". It is true that I don't have a route that matches that, but from what I understand, it should be POSTing to sessions#create, for which I have a route and an action. Why is it not doing this?
routes.rb
DinnerDash::Application.routes.draw do
get 'login', to: 'sessions#new', as: 'login'
get 'logout', to: 'sessions#destroy', as: 'logout'
get 'signup', to: 'users#new', as: 'signup'
root 'items#index'
resources :users
resources :password_resets
resources :sessions
resources :items
get "password/Resets"
get "password/create"
get "password/edit"
get "password/update"
end
sessions_controller.rb
class SessionsController < ApplicationController
def new
end
def create
user = login(params[:email], params[:password], params[:remember_me])
if user
redirect_back_or_to root_url, notice: 'Logged in!'
else
flash.now.alert = 'Unable to login.'
render :new
end
end
def destroy
logout
redirect_to root_url, notice: 'Logged out.'
end
end
rake routes
It's sorta hard to read, but the important points are that there's a GET /login, not a POST /login, and there's a GET and POST for /sessions_path.
login_path GET /login(.:format) sessions#new
logout_path GET /logout(.:format) sessions#destroy
signup_path GET /signup(.:format) users#new
root_path GET / items#index
users_path GET /users(.:format) users#index
POST /users(.:format) users#create
new_user_path GET /users/new(.:format) users#new
edit_user_path GET /users/:id/edit(.:format) users#edit
user_path GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
password_resets_path GET /password_resets(.:format) password_resets#index
POST /password_resets(.:format) password_resets#create
new_password_reset_path GET /password_resets/new(.:format) password_resets#new
edit_password_reset_path GET /password_resets/:id/edit(.:format) password_resets#edit
password_reset_path GET /password_resets/:id(.:format) password_resets#show
PATCH /password_resets/:id(.:format) password_resets#update
PUT /password_resets/:id(.:format) password_resets#update
DELETE /password_resets/:id(.:format) password_resets#destroy
sessions_path GET /sessions(.:format) sessions#index
POST /sessions(.:format) sessions#create
new_session_path GET /sessions/new(.:format) sessions#new
edit_session_path GET /sessions/:id/edit(.:format) sessions#edit
session_path GET /sessions/:id(.:format) sessions#show
PATCH /sessions/:id(.:format) sessions#update
PUT /sessions/:id(.:format) sessions#update
DELETE /sessions/:id(.:format) sessions#destroy
items_path GET /items(.:format) items#index
POST /items(.:format) items#create
new_item_path GET /items/new(.:format) items#new
edit_item_path GET /items/:id/edit(.:format) items#edit
item_path GET /items/:id(.:format) items#show
PATCH /items/:id(.:format) items#update
PUT /items/:id(.:format) items#update
DELETE /items/:id(.:format) items#destroy
password_Resets_path GET /password/Resets(.:format) password#Resets
password_create_path GET /password/create(.:format) password#create
password_edit_path GET /password/edit(.:format) password#edit
password_update_path GET /password/update(.:format) password#update
Try this
<%= simple_form_for :session, :url => sessions_path do |f| %>
# rest of the form
<% end %>
I have some troubles with routes in Rails 4 (rusty Rails user). I have the following routes for my Session controller:
controller :sessions do
get 'login' => :new
post 'login' => :create
delete 'logout' => :destroy
end
get "sessions/create"
get "sessions/destroy"
And I have a form that looks like this:
= form_tag do
.form_container
.field
= label_tag :name, "Namn:"
= text_field_tag :name, params[:name]
.field
= label_tag :password, "Lösenord:"
= password_field_tag :password, params[:password]
.actions
= submit_tag 'Login', :class => "submit_button"
And my session#create action looks like this:
def create
user = User.find_by(name: params[:name])
if user and user.authenticate(params[:password])
session[:user_id] = user.id
redirect_to root_path
else
redirect_to login_url, alert: "Invalid user/password combination"
end
end
And I get the following error:
No route matches [POST] "/login/create"
How should my routes look in this case?
I generally have a
resource :session
post 'login' => 'sessions#create'
get 'login' => 'sessions#new'
that creates
session POST /session(.:format) sessions#create
new_session GET /session/new(.:format) sessions#new
edit_session GET /session/edit(.:format) sessions#edit
GET /session(.:format) sessions#show
PUT /session(.:format) sessions#update
DELETE /session(.:format) sessions#destroy
login POST /login(.:format) sessions#create
GET /login(.:format) sessions#new
And after that just use the correct urls where needed
= form_tag login_path
...
That should do the trick
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.