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
Related
I'm using Acts_as_follower in a wishlist app I'm working on. I think it should be working, but the button to actually follow another user is not. The follow button is in my user index view, and I can see it there, but it doesn't respond to being clicked. Any help would be appreciated. Here's my code:
index.html.erb
<div class="container">
<% #users.each do |u| %>
<div class="col-sm-8 col-lg-4">
<div id="gifts">
<div class="box panel panel-default">
<h2><%= link_to u.name, u %></h2>
<p>
<table align="center" margin="5">
<tr>
<th style="padding-right: 10px"><%= button_to "View Wishlist", u, method: :get, class: "btn btn-primary" %></th>
<th>
<% if current_user.following?(u) %>
<%= form_tag user_unfollow_path(user_id: u.id), method: :delete, remote: true do %>
<%= button_tag 'unfollow', class: 'btn btn-primary' %>
<% end %>
<% else %>
<%= form_tag user_follow_path(user_id: u.id), method: :post, remote: true do %>
<%= button_tag 'follow', class: 'btn btn-success' %>
<% end %>
<% end %>
</th>
</tr>
</table>
<p>
</div>
</div>
</div>
<% end %>
</div>
followers_controller.rb
class FollowersController < ApplicationController
before_action :authenticate_user!
respond_to :js
def create
#user = User.find(params[:user_id])
current_user.follow(#user)
end
def destroy
#user = User.find(params[:user_id])
current_user.stop_following(#user)
end
end
users_controller.rb
class UsersController < ApplicationController
def show
#user = User.find(params[:id])
#gifts = #user.gifts
end
def index
#users = User.all
if params[:search]
#users = User.search(params[:search]).order("created_at DESC")
else
#users = User.all.order("created_at DESC")
end
end
def create
#user = User.find(params[:user_id])
current_user.follow(#user)
end
def destroy
#user = User.find(params[:user_id])
current_user.stop_following(#user)
end
private
def get_gifts
#gifts = Gift.find(params[:id])
end
end
routes.rb
Rails.application.routes.draw do
devise_for :users
resources :users, only: [:index] do
post :follow, :to => 'followers#create'
delete :unfollow, :to => 'followers#destroy'
end
resources :gifts
resources :wishlists
get 'wishlists/index'
get '/wishlists', :to => 'wishlists#index'
get '/users/:id', :to => 'users#show', :as => :user
get '/users', :to => 'users#index'
root "gifts#index"
end
user.rb (model)
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
acts_as_follower
acts_as_followable
has_many :gifts
belongs_to :wishlist
def self.search(search)
where("name ILIKE ?", "%#{search}%")
end
end
routes:
C:\Sites\gladlygift>rake routes
Prefix Verb URI Pattern Controller#A
tion
new_user_session GET /users/sign_in(.:format) devise/sessi
ns#new
user_session POST /users/sign_in(.:format) devise/sessi
ns#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessi
ns#destroy
user_password POST /users/password(.:format) devise/passw
rds#create
new_user_password GET /users/password/new(.:format) devise/passw
rds#new
edit_user_password GET /users/password/edit(.:format) devise/passw
rds#edit
PATCH /users/password(.:format) devise/passw
rds#update
PUT /users/password(.:format) devise/passw
rds#update
cancel_user_registration GET /users/cancel(.:format) devise/regis
rations#cancel
user_registration POST /users(.:format) devise/regis
rations#create
new_user_registration GET /users/sign_up(.:format) devise/regis
rations#new
edit_user_registration GET /users/edit(.:format) devise/regis
rations#edit
PATCH /users(.:format) devise/regis
rations#update
PUT /users(.:format) devise/regis
rations#update
DELETE /users(.:format) devise/regis
rations#destroy
user_follow POST /users/:user_id/follow(.:format) followers#cr
ate
user_unfollow DELETE /users/:user_id/unfollow(.:format) followers#de
troy
users GET /users(.:format) users#index
gifts GET /gifts(.:format) gifts#index
POST /gifts(.:format) gifts#create
new_gift GET /gifts/new(.:format) gifts#new
edit_gift GET /gifts/:id/edit(.:format) gifts#edit
gift GET /gifts/:id(.:format) gifts#show
PATCH /gifts/:id(.:format) gifts#update
PUT /gifts/:id(.:format) gifts#update
DELETE /gifts/:id(.:format) gifts#destro
wishlists GET /wishlists(.:format) wishlists#in
ex
POST /wishlists(.:format) wishlists#cr
ate
new_wishlist GET /wishlists/new(.:format) wishlists#ne
edit_wishlist GET /wishlists/:id/edit(.:format) wishlists#ed
t
wishlist GET /wishlists/:id(.:format) wishlists#sh
w
PATCH /wishlists/:id(.:format) wishlists#up
ate
PUT /wishlists/:id(.:format) wishlists#up
ate
DELETE /wishlists/:id(.:format) wishlists#de
troy
wishlists_index GET /wishlists/index(.:format) wishlists#in
ex
GET /wishlists(.:format) wishlists#in
ex
user GET /users/:id(.:format) users#show
GET /users(.:format) users#index
root GET / gifts#index
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
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
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
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