I have an issue with routing. I have a link to "new_company_path", but on the website it links to "site/dashboard". When I do a "rake routes" it gives the correct route for the link:
new_company GET /companies/new(.:format) {:action=>"new", :controller=>"companies"}
This is my "routes.rb":
match "/companies/:id/users" => "site#company_users", :as => :company_users
match "/company/:id/companies_user/new" => "companies_users#new"
resources :companies
get "site/index"
get "site/features"
get "site/dashboard", :as => "dashboard"
get "users_dashboard/show", :as => "users_dashboard"
get "login" => "sessions#new", :as => "login"
get "logout" => "sessions#destroy", :as => "logout"
Any reason why the link is not correct?
Related
Well the error is telling the truth, I have used the route name 'contact' twice, but one match is for a get request and the other for a post. I have been following the following tutorial for setting up Contact Form in Rails: http://matharvard.ca/posts/2011/aug/22/contact-form-in-rails-3/, and the author suggests adding the following to my routes file:
match 'contact' => 'contact#new', :as => 'contact', :via => :get
match 'contact' => 'contact#create', :as => 'contact', :via => :post
However that gives me the following error:
Invalid route name, already in use: 'contact' (ArgumentError)
Here is my own routes.rb file:
Fls::Application.routes.draw do
root 'welcome#index'
match 'contact' => 'contact#new', :via => :get
match 'contact' => 'contact#create', :as => 'contact', :via => :post
end
Do the following instead of above:
resource :contact, only: [:new, :create]
OR
get 'contact' => 'contact#new'
post 'contact' => 'contact#create', :as => 'contact'
I'm trying to route my User model sign up form to /sign_in. Tried several things and just used the example given in the devise docs.
I copied in to my routes.rb
devise_scope :users do
get "sign_in", to: "devise/sessions#new"
end
and rake routes says it should be working and shows what I expected.
http://s1.postimg.org/do335u8v3/Screen_Shot_2014_06_18_at_11_34_34.png
What's going wrong here?
Try this instead.
devise_for :users do
get '/users/sign_in', :to => 'devise/sessions#new', :as => :new_user_session
get '/users/sign_out', :to => 'devise/sessions#destroy', :as => :destroy_user_session
get "/users/sign_up", :to => "registrations#new"
end
I'm getting an error:
NoMethodError in Devise/registrations#new
undefined method 'user_registration_path'
at this line:
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
This is my routes:
devise_for :user, :controllers => { :registrations => "devise/registrations" }, :skip => [:registrations, :sessions] do
# devise/registrations
get 'signup' => 'devise/registrations#new', :as => :new_user_registration
post 'signup' => 'devise/registrations#create', :as => :custom_user_registration
end
Rake Routes:
new_user_registration GET /signup(.:format) devise/registrations#new
custom_user_registration POST /signup(.:format) devise/registrations#create
why am I getting the user_registration_path error?
Running rake routes, do you see in output smth like this:
user_registration POST /users(.:format) devise/registrations#create
I think if you wrote this line
post 'signup' => 'devise/registrations#create', :as => :custom_user_registration
Now you have:
custom_user_registration POST /signup(.:format) devise/registrations#create
And should use custom_user_registration_path(resource_name) instead of registration_path(resource_name)
You are skipping the registrations routes and not overriding them all with the custom routes you've defined. Remove :registrations from the skipped routes. In your routes.rb:
This
devise_for :user, :controllers => { :registrations => "devise/registrations" }, :skip => [:registrations, :sessions] do
should be
devise_for :user, :controllers => { :registrations => "devise/registrations" }, :skip => [:sessions] do
Or you can add these custom routes if you want the path to always be /signup:
devise_for :user, :controllers => { :registrations => "devise/registrations" }, :skip => [:registrations, :sessions] do
get 'signup' => 'devise/registrations#new', :as => :new_user_registration
post 'signup' => 'devise/registrations#create', as => :user_registration
delete 'signup' => 'devise/registrations#destroy', as => :destroy_user_registration
end
I don't advise changing the helper names (as => :whatever) since the devise controllers and views use them. It's fine to add new ones. I'm not sure you need to specify the controller either if it's the default.
On the sign up and forgot password views in Devise, if you get an error, it redirects you to the parent page.
So on the sign up page (/users/sign_up), if you get an error, it redirects you to /users and shows the error.
On the forgot password page (/users/password/new), if you get an error, it redirects you to /users/password and shows the error.
How can I change it so it does the same thing as the sign in page, if there's an error, it stays on the same page and shows the error.
I've looked through Devise and can't find where the redirect is.
Here's my routes for Devise:
devise_for :users, :skip => [:sessions]
as :user do
get 'signin' => 'devise/sessions#new', :as => :new_user_session
post 'signin' => 'devise/sessions#create', :as => :user_session
get 'signup' => 'devise/registrations#new', :as => :new_user
post 'signup' => 'devise/registrations#create', :as => :create_user_session
delete 'signout' => 'devise/sessions#destroy', :as => :destroy_user_session
get "/account" => "devise/registrations#edit"
end
I think the problem is that you have the post 'signup' named incorrectly. What path does your user signup form POST to?
post 'signup' => 'devise/registrations#create', :as => :create_user_session
Should be:
post 'signup' => 'devise/registrations#create', :as => :user_registration
Here's a look at my routes.rb which solved this issue:
as :user do
get "/signin" => "devise/sessions#new", :as => :new_user_session
post "/signin" => "devise/sessions#create", :as => :user_session
delete "/signout" => "devise/sessions#destroy", :as => :destroy_user_session
get "/signup" => "devise/registrations#new", :as => :new_user_registration
post '/signup' => 'devise/registrations#create', :as => :user_registration
end
It doesn't redirect you anywhere, those are the URLs that Devise posts to.
If you want to edit these URLs, see the wiki for a good starting point:
https://github.com/plataformatec/devise/wiki/How-To:-Change-the-default-sign_in-and-sign_out-routes
I started using custom routes w/Devise so that I could have my 'Sign In' and 'Sign Up' routes go to the same page. However, as soon as I followed the instruction from Devise about custom routes, it seems that every route now has to be explicitly specified. This has now broken my reset password links since that portion is handled by Devise.
What am I doing wrong here? You can see below that I've had to spell out everything for my User and UserSessions model. Shouldn't I only have to specify the ones I want to change?
devise_for :users, :controllers => { :sessions => "user_sessions" ,:registrations=>"users"},:skip => [:sessions] do
get 'users/sign_in' => 'user_sessions#new', :as => :new_user_session
get 'users/sign_up' => 'user_sessions#new', :as => :new_user_session
post 'users/sign_in' => 'user_sessions#create', :as => :user_session
post 'user_sessions' => 'user_sessions#create', :as => :app_sign_in
delete 'users/sign_out' => 'user_sessions#destroy', :as => :destroy_user_session
get 'users/sign_out' => 'user_sessions#destroy', :as => :destroy_user_session
post 'users/:id' => 'users#update', :as =>:update_user
get 'users' => 'users#index'
get 'users/:id/edit' => 'users#edit', :as => :edit_user
get 'users/:id' => 'users#show', :as => :show_user
delete 'users/:id' => 'users#destroy', :as => :destroy_user
end
Can you just use, not sure if this will work for you
devise_for :users
devise_for :users do
get '/users/sign_out' => 'devise/sessions#destroy'
end