I am having a problem with showing any of the views for my resource Field. I have this kind of association: User has one farm, farm has many fields.
My models:
User.rb
has_one :farm
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:token_authenticatable, :confirmable, :lockable, :timeoutable
attr_accessible :email, :password, :password_confirmation, :remember_me, :username
--
Farm.rb
belongs_to :user
has_many :fields
attr_accessible :name, :contact, :adress, :user_id
--
Field.rb
belongs_to :farm
attr_accessible :crop, :longitude, :latitude, :occupied, :farm_id
My rake routes:
user_farm_fields GET /users/:user_id/farm/:farm_id/fields(.:format)
{:action=>"index", :controller=>"fields"}
POST /users/:user_id/farm/:farm_id/fields(.:format)
{:action=>"create", :controller=>"fields"}
new_user_farm_field GET /users/:user_id/farm/:farm_id/fields/new(.:format) {:action=>"new", :controller=>"fields"}
edit_user_farm_field GET /users/:user_id/farm/:farm_id/fields/:id/edit(.:format){:action=>"edit", :controller=>"fields"}
user_farm_field GET /users/:user_id/farm/:farm_id/fields/:id(.:format) {:action=>"show", :controller=>"fields"}
PUT /users/:user_id/farm/:farm_id/fields/:id(.:format) {:action=>"update", :controller=>"fields"}
DELETE /users/:user_id/farm/:farm_id/fields/:id(.:format) {:action=>"destroy", :controller=>"fields"}
user_farms GET /users/:user_id/farm(.:format)
{:action=>"index", :controller=>"farms"}
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/:id/edit(.:format)
{:action=>"edit", :controller=>"farms"}
user_farm GET /users/:user_id/farm/:id(.:format)
{:action=>"show", :controller=>"farms"}
PUT /users/:user_id/farm/:id(.:format)
{:action=>"update", :controller=>"farms"}
DELETE /users/:user_id/farm/:id(.: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)
{:controller=>"home", :action=>"index"}
root /
{:controller=>"home", :action=>"index"}
My form for making new field:
<%= form_for([#user, #farm, #field]) do |f| %>
<% if #field.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#field.errors.count, "error") %> prohibited this field from being saved:</h2>
<ul>
<% #field.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :crop %><br />
<%= f.text_field :crop %>
</div>
<div class="field">
<%= f.label :longitude %><br />
<%= f.text_field :longitude %>
</div>
<div class="field">
<%= f.label :latitude %><br />
<%= f.text_field :latitude %>
</div>
<div class="field">
<%= f.label :occupied %><br />
<%= f.check_box :occupied %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
And my corresponding new method for field:
def new
#farm = Farm.where("user_id = ?", current_user).first
#user = current_user
#field = Field.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #field }
end
My routes.rb:
resources :users do
resources :farms, :path => 'farm' do
resources :fields
end
end
devise_for :users, :path => 'accounts'
After I try going to this link for creating new field :
<%= link_to 'New Field', new_user_farm_field_path(#user, #farm, #field) %>
I get this message:
undefined method `user_fields_path' for #<#<Class:0x496e1b0>:0x496b6d8>
On the surface level, the problem is likely to do with the form_for helper trying to set the url for your form without the correct nesting structure
You may wish to try this:
#app/views/fields/new.html.erb
<%= form_for [#user, #farm, #field], url: user_farm_fields_path do |f| %> #-> the create path
--
Nesting
There may be some deeper issues for you:
The corresponding route helper would be publisher_magazine_photo_url,
requiring you to specify objects at all three levels. Indeed, this
situation is confusing enough that a popular article by Jamis Buck
proposes a rule of thumb for good Rails design:
Resources should never be nested more than 1 level deep.
The caveat here, is that the docs stipulate you can do what you're doing (deep nesting) (I originally thought you shouldn't). Anyway, I think you might want to consider making your routes more "shallow", as to ensure they are more manageable.
To do this, I'd like you to consider the role of user in what you're doing:
User has_one Farm
This means that when you call resources :farms, you've got an antipattern - how can you select a farm if a user only has one? Surely a better way would be to remove any dependence on the user, create a singular resource for :farm, and then set the fields flow as you had them before
Here's how I'd do it:
#config/routes.rb
root "farms#index"
resources :farms, only: :index
resource :farm do #-> domain.com/farm (authentictable)
resources :fields #-> domain.com/farm/fields/new
end
#app/controllers/farms_controller.rb
Class FarmsController < ApplicationController
before_action :authenticate_user!, except: :index
def show
#farm = current_user.farm
end
def index
#farms = Farm.all
end
end
#app/models/user.rb
Class User < ActiveRecord::Base
has_one :farm
before_create :build_farm #-> creates farm if not already built
end
This will give you the ability to work everything back from being connected to the current_user:
#app/views/fields/new.html.erb
<%= form_for [#farm, #field] do |f| %>
<%= f.text_area :field_name %>
<%= f.submit %>
<% end %>
#app/controllers/fields_controller.rb
Class FieldsController < ApplicationController
before_action :authenticate_user!
def new
#farm = current_user.farm
#field = #farm.fields.new
end
def create
#farm = current_user.farm
#field = #farm.fields.new field_params
end
private
def field_params
params.require(:field).permit(:field_name)
end
end
Related
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
I am using the devise gem, and I have had user registration working in the past. However, something seems to have broken.
Mavens::Application.routes.draw do
devise_for :users
resources :events
resources :periods
resources :products
resources :cart_rows
resources :product_requests
resources :inqueries
match '/profile', to: 'static_pages#profile'
# resources :registrations do
# member do
# post :save_period
# end
# end
root :to => 'static_pages#home'
get "static_pages/home"
get "static_pages/about"
end
I should have used a different name than "registrations" but this registration is for an event, not a user. I've commented that out thinking it had something to do with this breaking.
Here is my rake 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
events GET /events(.:format) events#index
POST /events(.:format) events#create
new_event GET /events/new(.:format) events#new
edit_event GET /events/:id/edit(.:format) events#edit
event GET /events/:id(.:format) events#show
PUT /events/:id(.:format) events#update
DELETE /events/:id(.:format) events#destroy
periods GET /periods(.:format) periods#index
POST /periods(.:format) periods#create
new_period GET /periods/new(.:format) periods#new
edit_period GET /periods/:id/edit(.:format) periods#edit
period GET /periods/:id(.:format) periods#show
PUT /periods/:id(.:format) periods#update
DELETE /periods/:id(.:format) periods#destroy
products GET /products(.:format) products#index
POST /products(.:format) products#create
new_product GET /products/new(.:format) products#new
edit_product GET /products/:id/edit(.:format) products#edit
product GET /products/:id(.:format) products#show
PUT /products/:id(.:format) products#update
DELETE /products/:id(.:format) products#destroy
cart_rows GET /cart_rows(.:format) cart_rows#index
POST /cart_rows(.:format) cart_rows#create
new_cart_row GET /cart_rows/new(.:format) cart_rows#new
edit_cart_row GET /cart_rows/:id/edit(.:format) cart_rows#edit
cart_row GET /cart_rows/:id(.:format) cart_rows#show
PUT /cart_rows/:id(.:format) cart_rows#update
DELETE /cart_rows/:id(.:format) cart_rows#destroy
product_requests GET /product_requests(.:format) product_requests#index
POST /product_requests(.:format) product_requests#create
new_product_request GET /product_requests/new(.:format) product_requests#new
edit_product_request GET /product_requests/:id/edit(.:format) product_requests#edit
product_request GET /product_requests/:id(.:format) product_requests#show
PUT /product_requests/:id(.:format) product_requests#update
DELETE /product_requests/:id(.:format) product_requests#destroy
inqueries GET /inqueries(.:format) inqueries#index
POST /inqueries(.:format) inqueries#create
new_inquery GET /inqueries/new(.:format) inqueries#new
edit_inquery GET /inqueries/:id/edit(.:format) inqueries#edit
inquery GET /inqueries/:id(.:format) inqueries#show
PUT /inqueries/:id(.:format) inqueries#update
DELETE /inqueries/:id(.:format) inqueries#destroy
profile /profile(.:format) static_pages#profile
root / static_pages#home
static_pages_home GET /static_pages/home(.:format) static_pages#home
static_pages_about GET /static_pages/about(.:format) static_pages#about
And here is the view page:
<h2>Sign Up</h2>
<%= link_to "Sign in", new_session_path(resource_name) %> | <%= link_to "Forgot your password?", new_password_path(resource_name) %><br />
<br />
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<%= f.label :email %>
<%= f.email_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %>
<%= f.label :first_name %>
<%= f.text_field :first_name%>
<%= f.label :last_name %>
<%= f.text_field :last_name%>
<%= f.label :license_number %>
<%= f.text_field :license_number %>
<%= f.label :state %>
<%= f.select :state, User::STATES%>
<%= f.label :specialty %>
<%= f.select :specialty, User::SPECIALTY%>
<br />
<%= f.submit "Sign up" %>
<% end %>
Any ideas on what could have changed to break the routing?
I did a rails d on the model and controller and that seemed to get it working. Pro_tip, always prefix variable names with something so they don't compete with names of other systems you pull in!
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
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
I've gone through the getting started with rails tutorial and now i'm trying to setup another project using the devise gem for authentication. Basically all i want is to have a home page, a page where the user can fill out a form to essentially signup, and then a secure page that does the authentication.
right now i'm having problems when i navigate to the signup page. Here's the error i'm receiving:
NoMethodError in Signup#index
Showing /Users/tomcaflisch/Sites/project2/app/views/signup/_form.html.erb where line #1 raised:
undefined method `users_path' for #<#<Class:0x007fa734b3e510>:0x007fa734b3a910>
Extracted source (around line #1):
1: <%= form_for(#user) do |f| %>
2: <% if #user.errors.any? %>
3: <div id="errorExplanation">
4: <h2><%= pluralize(#user.errors.count, "error") %> prohibited this post from being saved: </h2>
signup_controller.rb:
class SignupController < ApplicationController
def index
#user = User.new
end
def new
#user = User.new
respond_to do |format|
format.html
end
end
end
user.rb model:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :username
end
../views/signup_form.html.erb:
<%= form_for(#user) do |f| %>
<% if #user.errors.any? %>
<div id="errorExplanation">
<h2><%= pluralize(#user.errors.count, "error") %> prohibited this post from being saved: </h2>
<ul>
<% #user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :username %><br />
<%= f.text_field :username %>
</div>
<div class="field">
<%= f.label :password %><br />
<%= f.text_field :password %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
routes.rb:
get "signup/index"
devise_for :users
get "index/index"
root :to => 'index#index'
$ bundle exec rake routes | grep user:
new_user_session GET /users/sign_in(.:format) {:action=>"new", :controller=>"devise/sessions"}
user_session POST /users/sign_in(.:format) {:action=>"create", :controller=>"devise/sessions"}
destroy_user_session DELETE /users/sign_out(.:format) {:action=>"destroy", :controller=>"devise/sessions"}
user_password POST /users/password(.:format) {:action=>"create", :controller=>"devise/passwords"}
new_user_password GET /users/password/new(.:format) {:action=>"new", :controller=>"devise/passwords"}
edit_user_password GET /users/password/edit(.:format) {:action=>"edit", :controller=>"devise/passwords"}
PUT /users/password(.:format) {:action=>"update", :controller=>"devise/passwords"}
cancel_user_registration GET /users/cancel(.:format) {:action=>"cancel", :controller=>"devise/registrations"}
user_registration POST /users(.:format) {:action=>"create", :controller=>"devise/registrations"}
new_user_registration GET /users/sign_up(.:format) {:action=>"new", :controller=>"devise/registrations"}
edit_user_registration GET /users/edit(.:format) {:action=>"edit", :controller=>"devise/registrations"}
PUT /users(.:format) {:action=>"update", :controller=>"devise/registrations"}
DELETE /users(.:format) {:action=>"destroy", :controller=>"devise/registrations"}
It looks like you're mixing two things: devise provides it's own signup / registration pages, and you've also created your own.
Sometimes that's appropriate, but many times the default devise pages are good enough -- or at least good enough to start with.
I'd recommend you begin by trying to implement devise with it's own pages -- leaving your signin and signup pages alone for now. You don't see the devise pages because they are hidden inside the gem.
If you want to customize them, you can get the devise pages installed in your project (in a haml format) by following the steps here:
https://github.com/plataformatec/devise/wiki/How-To:-Create-Haml-and-Slim-Views
Because you are creating you're own signup controller, I believe devise routes are not routing where you like
post your routes file and lets see if we can get your routes straightened out.
It is looking for Signup#index .. It should be looking for SignupController#index
in your routes.rb you want to have
root :to => 'signup#index'
Also, undefined method users_path is missing its route to get the users or most likely missing resources :users from your routes.rb
For a new user you want something like form_for(User.new) which will hit UsersController#new
__
Or do it this way
1)
For views/users/new.html.erb you want
<%= form_for(#user) do |f| %>
<%= render 'fields', :f => f %>
....
with a route to it something like get '/signup', :to => 'users#new' and link_to 'Signup', signup_path
2)
For UsersController add
def new
#user = User.new
..
end