Devise user authentication on Rails 3: routing error on default views - ruby-on-rails-3

I followed the basic steps to add authentication to Rails using Devise from their page, but every time I try to visit a default page (such as the Sign In or Sign Up pages), I get:
Routing Error
No route matches {:controller=>"devise/Home"}
This happens whether I link to the page in a view using
link_to('Register', new_user_registration_path)
or just visit "/users/sign_up".
This is a different error from when I visit a page with no route defined (No route matches [GET] "/users/bad_example"), and
devise_for :users
is already present in my routes.rb. I have even tried generating views (rails g devise:views) to no avail. It looks like Devise isn't generating/using a controller or some such. How do I go about fixing this?
Here are some files that may help:
routes.rb
rake routes output

It turns out the problem was not with Devise, but with my routes file. I have fixed my routes file as seen at http://guides.rubyonrails.org/routing.html and now this works properly.

Related

Devise redirection issue

I'm using devise in my rails3 project. I need to redirect user to different page once logged-in. Without overriding sessions controller how can I do this?
Question looks like a duplicate of https://stackoverflow.com/a/12854498/790737
Try putting this in you ApplicationController:
def after_sign_in_path_for(resource)
different_page_path # this should be a path helper
end
I assume you know how to use path and url helpers, and that you can use
rake routes
to list them. Good luck.

Rails "automatic" routes don't work, must specify with get "controller/method" every time?

I did
rails generate controller home index
But it adds this line to my routes.rb
get "home/index"
I thought Rails could deduce controller/method from the URL automatically? Why do I need to specify every get/post page?
Here's my complete routes.rb file:
Callisto2::Application.routes.draw do
root :to => "home#index"
resources :assets
end
root "/" works fine. so does /assets/*.
What's the problem with /home/index? I get the error:
Routing Error
No route matches [GET] "/home/index"
Try running rake routes for more information on available routes.
rake routes (run as apache user) gives me the following output:
root / home#index
Thanks for any clarifications. Not sure what I'm missing.
Edit: I didn't make this clear: I manually removed get /home/index from routes.rb to keep that file clean.
Rails used to add the so called catch all route at the bottom of your routes file:
match ':controller(/:action(/:id(.:format)))'
There was nothing 'automatic' or magical about these urls, just that every rails app started out with this route in their routes.rb
This has fallen out of favour, at least partially because it makes everything accessible over get, whereas
resources :books
Adds each route with the appropriate http verb. Listing routes explicitly is also a lot less verbose than when rails started out.
If your controller is home and the action is index your path is just /home.
You can find more information here.

Issues with Devise routes - error in view pages

I had an existing User model before I installed Devise so I followed the instructions here.
I even generated the devise views. But when I am at my localhost and type in localhost:3000/users/sign_in or any of the other routes available to me via devise, it doesn't work.
Also, my existing RESTful routes for users that I got from using
resources :users
are no longer available unless I have both:
devise_for :users
resources :users
but I thought I was supposed to delete the resources :users when I had the devise_for :users line in my routes file.
What is going on? Does anyone have any idea?
EDIT
The issue is when I go to users/password, it tells me that there is no user with ID=password, but this route is available to me.
I also have issues with the users/sign_in and users/sign_out. It redirects back to my root path for some reason. Sometimes it works, other times it doesn't and I am not sure why. The log looks like this:
Started GET "/users/sign_in" for 127.0.0.1 at 2011-09-16 19:05:43 -0400
Processing by Devise::SessionsController#new as HTML
User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
Redirected to http://localhost:3000/
What exactly doesn't work here? Are you getting anything useful in the logs or elsewhere? Also, have you setup your :users model to include the devise :recoverable, :authenticateable, ..., etc.?
You need both devise_for and resources defined in your config/routes.rb. devise_for sets up routes that will go to the devise controllers, but does not handle anything else. So you will still need normal resourceful routes for the user model to do add, update, delete, etc.

creating a launch page in rails app

I am planning to create an app in rails but first I want to make a launch page. Having never made a launch page I am curious as to how others are doing it?
Do you creae a small rails application with controller and model that just collects email addresses? and then deploy the rails app? I'd prefer this way but it seems like an overkill to deploy a rails app just for a launch page...?
Also, how do you modify the routes file so that if users type anything after the url then only page that shows up is the laungh page.
Meaning, if my launch page is at http://mycoollaunchpage.com then if users mess around and type http://mycoollaunchpage.com/lkjlkjljk then it should redirect back to http://mycoollaunchpage.com
Your idea sounds good. Just a page with an email signup form would work well.
To redirect back to your home page, make a route glob in your routes.rb file, and have an action in your controller that just redirects back to your root.
# in routes.rb
match "*whatever", :controller => 'pages', :action => 'redirect_to_root'
# in your pages_controller.rb file
def redirect_to_root
redirect_to "/"
end
There is an awesome rails plugin available for this very requirement of yours ;)
https://github.com/vinsol/Launching-Soon/

Rails 3 namespace routing

I have an admin namespace and a scaffold of companies placed out of the admin namespace.
I wanted to put the companies into admin namespace
Then I put the companies_controller into admin directory and changed the definition to
class Admin::CompaniesController < Admin::AdminController
and put the companies views into the admin directory in /app/views/
and put the companies_helper into admin directory and now it looks as follows:
module Admin::CompaniesHelper
end
The namespace in routes.rb:
namespace :admin do
root :to => "companies#index"
resources :companies
end
When I go to localhost:3000/admin I get this error:
undefined method `company_path' for #:0xb696b408>
Now please tell me how to edit the links to make the links work properly?
When you moved the controller in to the admin namespace you changed routes to the links created in the scaffolded templates. For example if your templates use company_path the links would change to admin_company_path.
To view the routes within your application at any given point in time, run "rake routes" from the command line within the root of your rails application. This will show you all the routes within you application
Since company is under the namespace admin you have to prefix the path with admin.
Like so:
admin_company_path(#company)
See this Rails guide for more info on Rails routing and namespaces.
I got the kind of ugly solution but it works. I generated a new scaffold but differently:
rails generate scaffold Admin::Companies
instead of
rails generate scaffold Companies
but I still don't understand how the helpers make url for the resources :(