Missing :id in my routes in rails4 - ruby-on-rails

hi,
I am having a problem with my routes. Just noticed a strange thing. I have no :id in my show/edit/... routes.
This is my rake.routes output:
WARNING: Nokogiri was built against LibXML version 2.9.0, but has dynamically loaded 2.9.2
Prefix Verb URI Pattern Controller#Action
new_person_session GET /persons/sign_in(.:format) devise/sessions#new
person_session POST /persons/sign_in(.:format) devise/sessions#create
destroy_person_session DELETE /persons/sign_out(.:format) devise/sessions#destroy
person_password POST /persons/password(.:format) devise/passwords#create
new_person_password GET /persons/password/new(.:format) devise/passwords#new
edit_person_password GET /persons/password/edit(.:format) devise/passwords#edit
PATCH /persons/password(.:format) devise/passwords#update
PUT /persons/password(.:format) devise/passwords#update
supplier_password POST /suppliers/password(.:format) devise/passwords#create
new_supplier_password GET /suppliers/password/new(.:format) devise/passwords#new
edit_supplier_password GET /suppliers/password/edit(.:format) devise/passwords#edit
PATCH /suppliers/password(.:format) devise/passwords#update
PUT /suppliers/password(.:format) devise/passwords#update
facilitator_password POST /facilitators/password(.:format) devise/passwords#create
new_facilitator_password GET /facilitators/password/new(.:format) devise/passwords#new
edit_facilitator_password GET /facilitators/password/edit(.:format) devise/passwords#edit
PATCH /facilitators/password(.:format) devise/passwords#update
PUT /facilitators/password(.:format) devise/passwords#update
customer_password POST /customers/password(.:format) devise/passwords#create
new_customer_password GET /customers/password/new(.:format) devise/passwords#new
edit_customer_password GET /customers/password/edit(.:format) devise/passwords#edit
PATCH /customers/password(.:format) devise/passwords#update
PUT /customers/password(.:format) devise/passwords#update
cancel_customer_registration GET /customers/cancel(.:format) devise/registrations#cancel
customer_registration POST /customers(.:format) devise/registrations#create
new_customer_registration GET /customers/sign_up(.:format) devise/registrations#new
edit_customer_registration GET /customers/edit(.:format) devise/registrations#edit
PATCH /customers(.:format) devise/registrations#update
PUT /customers(.:format) devise/registrations#update
DELETE /customers(.:format) devise/registrations#destroy
home_index GET /home/index(.:format) home#index
root GET / home#index
suppliers_index GET /suppliers/index(.:format) suppliers#index
facilitators_index GET /facilitators/index(.:format) facilitators#index
customers_index GET /customers/index(.:format) customers#index
new_customers GET /customers/new(.:format) customers#new
customers_newCustomer POST /customers/newCustomer(.:format) customers#newCustomer
customers_editCustomer GET /customers/editCustomer(.:format) customers#editCustomer
parties POST /parties(.:format) parties#create
new_parties GET /parties/new(.:format) parties#new
edit_parties GET /parties/edit(.:format) parties#edit
GET /parties(.:format) parties#show
PATCH /parties(.:format) parties#update
PUT /parties(.:format) parties#update
DELETE /parties(.:format) parties#destroy
parties_index GET /parties/index(.:format) parties#index
visitors_new POST /visitors/new(.:format) visitors#new
visitors_create POST /visitors/create(.:format) visitors#create
this is my routes.rb:
Rails.application.routes.draw do
# devise_for :people
devise_for :persons, :skip => :registrations
devise_for :suppliers, :facilitators, skip: [:registrations, :sessions]
devise_for :customers, :skip => :sessions
# routes for all users
authenticated :persons do
end
# routes only for customers
authenticated :customers, lambda {|u| u.type == "Customer"} do
end
# routes only for companies
authenticated :facilitators, lambda {|u| u.type == "Facilitator"} do
end
# routes only for suppliers
authenticated :suppliers, lambda {|u| u.type == "Supplier"} do
end
# mde toevoegeingen
get 'home/index'
root to: "home#index"
get 'suppliers/index'
get 'facilitators/index'
get 'customers/index'
resource :customers, :only => :new
match 'customers/newCustomer', to: 'customers#newCustomer', via: :post
match 'customers/editCustomer', to: 'customers#editCustomer', via: :get
resource :parties
get 'parties/index'
match 'visitors/new', to: 'visitors#new', via: :post
match 'visitors/create', to: 'visitors#create', via: :post
end
My urls show a '.' instead of a '/' before the 'id'. this is an example url for the show method of a model:
http://localhost:3000/parties.2
The strange thing is that the normal routing is working. Strange because the dot is intended for defining the format.
Now I need the format becase of a remote: true behaviour I am implementing I am getting into troubles.
What did I do wrong?
regards Martijn

Try to change:
resource :parties
To:
resources :parties

Just like Brad Werth said in comments.
the plural resources
resources :photos
creates
GET /photos Photos index display a list of all photos
GET /photos/new Photos new return an HTML form for creating a new photo
POST /photos Photos create create a new photo
GET /photos/1 Photos show display a specific photo
GET /photos/1/edit Photos edit return an HTML form for editing a photo
PUT /photos/1 Photos update update a specific photo
DELETE /photos/1 Photos destroy delete a specific photo
in contrast to Singular Resources
You can also apply RESTful routing to singleton resources within your
application. In this case, you use map.resource instead of
map.resources and the route generation is slightly different. For
example, a routing entry of
resource :geocoded
GET /geocoder/new Geocoders new return an HTML form for creating the new geocoder
POST /geocoder Geocoders create create the new geocoder
GET /geocoder Geocoders show display the one and only geocoder resource
GET /geocoder/edit Geocoders edit return an HTML form for editing the geocoder
PUT /geocoder Geocoders update update the one and only geocoder resource
DELETE /geocoder Geocoders destroy delete the geocode resource

Related

Can I use "devise_for", with "only" and "excepted"?

I'm building a Rails website, and setting up the devise routes. So far, I have no trouble and everything works. However, since the website is divided between the administration and the visitor place, I would like the user to be able to edit his profile from a path that looks like /admin/users/1/edit and sign in/out from /user/sign_in or user/sign_out (without the admin prefixe).
So far, I managed to do either with or without the prefix, but not both at the same time.
Here is the relevant part of my route file:
devise_for :users, only: %w['session#new session#destroy']
get '/admin' => 'home#admin', as: :admin
authenticate :user do
scope '/admin' do
resource :basics
resources :portfolios
resources :articles
devise_for :users, excepted: %w['sessions#new session#destroy']
end
end
And here is the result of rake routes:
new_user_session GET /admin/users/sign_in(.:format) devise/sessions#new
user_session POST /admin/users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /admin/users/sign_out(.:format) devise/sessions#destroy
new_user_password GET /admin/users/password/new(.:format) devise/passwords#new
edit_user_password GET /admin/users/password/edit(.:format) devise/passwords#edit
user_password PATCH /admin/users/password(.:format) devise/passwords#update
PUT /admin/users/password(.:format) devise/passwords#update
POST /admin/users/password(.:format) devise/passwords#create
cancel_user_registration GET /admin/users/cancel(.:format) devise/registrations#cancel
new_user_registration GET /admin/users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /admin/users/edit(.:format) devise/registrations#edit
user_registration PATCH /admin/users(.:format) devise/registrations#update
PUT /admin/users(.:format) devise/registrations#update
DELETE /admin/users(.:format) devise/registrations#destroy
POST /admin/users(.:format) devise/registrations#create
How can do this?
Thank you in advance
I believe it may just be a typo, but try changing:
devise_for :users, excepted: %w[sessions#new session#destroy]
to:
devise_for :users, except: %w[sessions#new session#destroy]

How to use Devise both in the site and in the Admin Pages

I'm new to RoR and Devise and i'm stuck in User Authentications with Devise. I'm developing a web site, which has admin pages in it. My structure looks like this:
app
|-controllers
|-admin
|- user_list_controller.rb //Every user crud operations are in it.
|-views
|-admin
|-user_list
|-new.html.erb
|-edit.html.erb
|-devise //Also have devise views
UserListController:
class Admin::UserListController < ApplicationController
layout 'admin/admin'
def index
#user_list = User.all
end
def new
#user = User.new
end
def edit
end
def delete
end
def create_user
end
end
What I want to do is that, I want to use Devise methods under this controller but this is where I stuck in. I created a UserController which base is Devise::RegistrationsController. But this time I got this error,
Could not find devise mapping for path "/admin/create_user". This may
happen for two reasons: 1) You forgot to wrap your route inside the
scope block. For example: devise_scope :user do get "/some/route" =>
"some_devise_controller" end 2) You are testing a Devise controller
bypassing the router. If so, you can explicitly tell Devise which
mapping to use: #request.env["devise.mapping"] =
Devise.mappings[:user]
It looks like a route.rb error and my route file is:
Rails.application.routes.draw do
devise_for :users
devise_scope :user do
# post "admin/add_user" =>"admin/user_list#create_user", as: :adduser
end
namespace :admin do
root to: 'admin#index'
get 'user_list', :to => 'user_list#index'
post 'create_user', :to => "user#create"
get 'new_user', :to => 'user_list#new'
get 'user_proposals', :to => 'user_proposal_forms#index'
get 'user_appointments', :to => 'user_appointments#index'
get 'brands', :to => 'brands#index'
get 'brand_makes', :to => 'brand_makes#index'
get 'make_types', :to => 'make_types#index'
end
end
And the result of rake routes is this:
Prefix Verb URI Pattern Controller#Action 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
admin_root GET /admin(.:format) admin/admin#index
admin_user_list GET /admin/user_list(.:format) admin/user_list#index
admin_create_user POST /admin/create_user(.:format) admin/user#create
admin_new_user GET /admin/new_user(.:format) admin/user_list#new
admin_user_proposalsGET /admin/user_proposals(.:format) admin/user_proposal_forms#index
admin_user_appointments GET /admin/user_appointments(.:format) admin/user_appointments#index
admin_brands GET /admin/brands(.:format) admin/brands#index
admin_brand_makes GET /admin/brand_makes(.:format) admin/brand_makes#index
admin_make_types GET /admin/make_types(.:format) admin/make_types#index
It looks messy, sorry for that. Finally my form_for looks like this:
<%= simple_form_for #user, url: admin_create_user_path, class: "form-horizontal" do |f| %>
<%= render(:partial => "form", :locals => {:f => f}) %>
<% end %>
So where did I make a mistake? I've read all the documents in Devise, tried so many things but couldn't solve the problem.
In routes file use
devise_for :users, :controllers => { registrations: 'registrations' }

Rails 4:Error in setting up SignIn Page as root page using Devise

I am using Devise Gem for authentication but for the index/root page for my application I want to use sign_in page of devise gem.
I have used the following code in the config/routes.rb file
root 'devise/sessions#new'
I am getting the following error
Could not find devise mapping for path "/". This may happen for two reasons: 1) You forgot to wrap your route inside the scope block. For example: devise_scope :user do get "/some/route" => "some_devise_controller" end 2) You are testing a Devise controller bypassing the router. If so, you can explicitly tell Devise which mapping to use: #request.env["devise.mapping"] = Devise.mappings[:user]
Here is snapshot of my rake routes output
Prefix Verb URI Pattern Controller#Action
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#destro
workout_schedules_find POST /workout_schedules/find(.:format) workout_schedules#find
workout_schedules GET /workout_schedules(.:format) workout_schedules#index
POST /workout_schedules(.:format) workout_schedules#create
new_workout_schedule GET /workout_schedules/new(.:format) workout_schedules#new
edit_workout_schedule GET /workout_schedules/:id/edit(.:format) workout_schedules#edit
workout_schedule GET /workout_schedules/:id(.:format) workout_schedules#show
PATCH /workout_schedules/:id(.:format) workout_schedules#update
PUT /workout_schedules/:id(.:format) workout_schedules#update
DELETE /workout_schedules/:id(.:format) workout_schedules#destroy
Try to set your route file like this:
devise_scope :user do
authenticated :user do
root 'home#dashboard', as: :authenticated_root
end
unauthenticated do
root 'devise/sessions#new', as: :unauthenticated_root
end
end
Devise provides some inbuilt methods like authenticated, unauthenticated etc. So as per your requirement, you can set different root for logged in users and non-logged in users.
:as will create custom named url helpers like authenticated_root_path and unauthenticated_root_path
If you will run rake routes, you will get:
authenticated_root GET / home#dashboard
unauthenticated_root GET / devise/sessions#new
Here's my working example of how to solve this:
Rails.application.routes.draw do
# This is the fix.
devise_for :users, skip: :all
devise_scope :user do
get "/users/sign_in", to: "devise/sessions#new", as: :new_user_session
post "/users/sign_in", to: "devise/sessions#create", as: :user_session
delete "/users/sign_out", to: "devise/sessions#destroy", as: :destroy_user_session
get "/users/password/new", to: "devise/passwords#new", as: :new_user_password
get "/users/password/edit", to: "devise/passwords#edit", as: :edit_user_password
patch "/users/password", to: "devise/passwords#update", as: :user_password
put "/users/password", to: "devise/passwords#update"
post "/users/password", to: "devise/passwords#create"
get "/users/cancel", to: "devise/registrations#cancel", as: :cancel_user_registration
get "/users/sign_up", to: "devise/registrations#new", as: :new_user_registration
get "/users/edit", to: "devise/registrations#edit", as: :edit_user_registration
patch "/users", to: "devise/registrations#update", as: :user_registration
put "/users", to: "devise/registrations#update"
delete "/users", to: "devise/registrations#destroy"
post "/users", to: "devise/registrations#create"
end
end

Devise with Rails 4: user show route doesn't exist

I am using the devise gem to handle all the sign up and sign in stuff. But I also want to add user profiles to my application, so I generated a user controller with only a show action. Then I added get 'users/:id' => 'users#show' to routes.rb. In fact, typing /users/1 works, but I can't find a way to name the route. What I want is to get something like show_user_path or user_path so I can link to a given user's content and show that user's profile.
Here is my routes.rb
Pinteresting::Application.routes.draw do
resources :pins
get 'users/:id' => 'users#show'
devise_for :users
root "pins#index"
get "about" => "pages#about"
And here are the routes I get with it (I highlighted the one I expect to be something like show_user_path):
pins_path GET /pins(.:format) pins#index
POST /pins(.:format) pins#create
new_pin_path GET /pins/new(.:format) pins#new
edit_pin_path GET /pins/:id/edit(.:format) pins#edit
pin_path GET /pins/:id(.:format) pins#show
PATCH /pins/:id(.:format) pins#update
PUT /pins/:id(.:format) pins#update
DELETE /pins/:id(.:format) pins#destroy
#this is the one I want a path! GET /users/:id(.:format) users#show
new_user_session_path GET /users/sign_in(.:format) devise/sessions#new
user_session_path POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session_path DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password_path POST /users/password(.:format) devise/passwords#create
new_user_password_path GET /users/password/new(.:format) devise/passwords#new
edit_user_password_path 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_path GET /users/cancel(.:format) devise/registrations#cancel
user_registration_path POST /users(.:format) devise/registrations#create
new_user_registration_path GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration_path 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
root_path GET / pins#index
about_path GET /about(.:format) pages#about
For devise, User is not the resource, it's just a scope. What devise cares about is authentication.
Although the paths are nested under /user, you will notice that the resources that are defined are actually things like sessions, registrations, passwords...
Just add resources :users in your routes and create a UsersController (and the views).
If you don't want to create all the resources for users and just be able to user user_path(user) with your get 'users/:id, you can name that route using the as option, like this:
get 'users/:id' => 'users#show', as: user
The answer above is great but I think it's worth noting here that if you don't want to create a different controller, but want to add an action to, say, your registrations controller that you inherit from the Devise::RegistrationsController, then you need to use the devise scope block:
devise_scope :user do
get 'users/:id' => 'registrations#show', as: user
end
the answer supposed to be like this:
get 'user/:id' => 'users#show', as: :user

devise redirect on login error: ActiveRecord::RecordNotFound in DocumentsController#show

I 'm trying to redirect to a specific page after sign_in & sign_up. I have two models users & resources. After sign_in I want to show list of documents that belong to the user.
If there are no documents then it should redirect to users/:id/document#create otherwise users/:id/document#index.
Any suggestions how do i do that?
I get the following error for the redirect path defined in my controller.
ActiveRecord::RecordNotFound in DocumentsController#show
Couldn't find Document without an ID
Application_controller.rb
def after_sign_in_path_for(user)
user_documents_url(user)
end
Routes.rb
root :to => 'home#index'
devise_for :users
resources :users do
resources :documents
end
devise_for :users do get '/users/sign_out' => 'devise/sessions#destroy' end
Routes
root / home#index
new_user_session GET /login(.:format) devise/sessions#new
user_session POST /login(.:format) devise/sessions#create
destroy_user_session DELETE /logout(.:format) devise/sessions#destroy
user_password POST /password(.:format) devise/passwords#create
new_user_password GET /password/new(.:format) devise/passwords#new
edit_user_password GET /password/edit(.:format) devise/passwords#edit
PUT /password(.:format) devise/passwords#update
cancel_user_registration GET /cancel(.:format) devise/registrations#cancel
user_registration POST / devise/registrations#create
new_user_registration GET /sign_up(.:format) devise/registrations#new
edit_user_registration GET /edit(.:format) devise/registrations#edit
PUT / devise/registrations#update
DELETE / devise/registrations#destroy
user_confirmation POST /confirmation(.:format) devise/confirmations#create
new_user_confirmation GET /confirmation/new(.:format) devise/confirmations#new
GET /confirmation(.:format) devise/confirmations#show
user_documents POST /users/:user_id/documents(.:format) documents#create
new_user_documents GET /users/:user_id/documents/new(.:format) documents#new
edit_user_documents GET /users/:user_id/documents/edit(.:format) documents#edit
GET /users/:user_id/documents(.:format) documents#show
PUT /users/:user_id/documents(.:format) documents#update
/user/:user_id/documents(.:format) user/documents#index
Thanks
The code itself has no problem as I see. But you need to build some "documents" data before this test, either by FactoryGirl or stubs.
Because you really don't have "documents" data for any users, of course Rails gave you to RecordNotFound error.
use this method according to following code
def after_sign_in_path_for(resource_or_scope)
// redirect path
end
i hope this will help
I fixed my problem by following this question.Following is my routes.rb
resources :users do
resources :documents, only: [:index]
end
resource :documents, except: [:index]
this gives me document#index path as well as document#new path.

Resources