Why does Localhost:3000 points to default page? - ruby-on-rails

I'm new to Rails. With rails running, opening localhost:3000 takes me to default "welcome aboard" page.
Rake routes command puts out a message that I don't have any routes defined, but I didn't change anything in config/routes.rb.
I tried to download back my github repository, but the problem persists. Any idea, please?
#MarcinAdamczyk, #RichPeck. I should say that localhost did work before. this is what I have:
1)
Pinteresting::Application.routes.draw do
resources :pins
devise_for :users
devise_for :installs
root "pages#home"
get "about" => "pages#about"
2)
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
end
def home
end
3)
class PagesController < ApplicationController
def home
end
def about
end
end

If you are using Rails <4 then you need to remove index.html from public folder and set root to: 'controller#method' in config/routes.rb
If its Rails 4 then only setting root route should be enough.

Routes
You'll need to change your routes to have the following:
#config/routes.rb
root "application#index"
You can then create & corresponding controller action:
#app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
def index
end
end
#app/views/application/index.html.erb
Test
This should fix your immediate issue, judging from what you've posted.

Related

Rails routes for a static landing page if user not logged in

In my application I am using the Devise authentication gem which works really well. I also have built a static landing page which I currently have in the /public directory.
I can of course browser to localhost:3000/landing and see the page (as per the route below) but what I'm trying to achieve but cannot seem to figure out is how to setup my routes.rb file so that the landing.html file is the root, but when a user is logged in, their root becomes companies#index.
Here is my routes.rb file
Rails.application.routes.draw do
devise_for :users
get 'dashboard/index'
get '/landing', :to => redirect('/landing.html')
root 'companies#index'
resources :companies do
resources :shareholders
resources :captables do
post :subscribe_to_captable
resources :events do
post :lock_event
post :unlock_event
resources :transactions
end
end
end
end
Here is my application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :authenticate_user!
end
One way is to put the redirect inside your CompaniesController.rb index method do this.
CompanesController.rb (or whatever it is called)
def index
unless user_signed_in?
redirect_to landing_pages_path (or whatever the name of the route is)
end
end
Then change the route in the routes.rb file.
get '/landing', to: 'companies#landing' * change companies here to the name of the controller holding the landing page's method if it is not in the companies controller.

From the Michael Hartl book, the route "/static_pages/help" won't show

Whenever I try to navigate to this link,
it does not change to this view. It just continues to display the root route.
Please can someone point out what I'm doing wrong.
class StaticPagesController < ApplicationController
def home
end
def help
end
end
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
def hello
render html: "hello, world!"
end
end
Rails.application.routes.draw do
get 'static_pages/home'
get 'static_pages/help'
root 'application#hello'
end
Try to specify the concerned controller, when done there is no reason for the following not to work :
Rails.application.routes.draw do
root 'application#hello'
get 'static_pages/home', to: 'static_pages#home'
get 'static_pages/help', to: 'static_pages#help'
end
I don't know if everyone's does this, but it seems to me preferred to put the root atop.

Rails keeps displaying home page on root even when I delete the method in the Pages Controller

This is more of a question than a real issue, but I created a new rails project. I've changed the
Rails.application.routes.draw do
get 'pages/about'
get 'pages/contact'
devise_for :users
root to: 'pages#home'
end
and my Pages controller looks like this
class PagesController < ApplicationController
skip_before_action :authenticate_user!, only: [:home]
def home
end
def about
end
def contact
end
end
My homepage gets displayed even if I remove the home method.
I just wanted to know more about how this works.
I've looked around before posting and couldn't find an answer.
This is my first post here.
Thanks in advance
This is part of Rails 'convention over configuration' mantra. See Rendering by Default: Convention Over Configuration in Action
From the guide:
By default, controllers in Rails automatically render views with names that correspond to valid routes.
So, even if the home action is undefined, Rails will still render the home view because there is a valid route.

Devise in rails 4 doesn't show sign_in form

I have a problem with the devise gem, I have this controller.
class AdminController < ApplicationController
before_action :authenticate_user!
def index
end
def per
end
def po
end
end
When redirect to sign_in form , shows nothing
sign_in form
These are my routes:
match 'po' => 'admin#po', :via => :get
match 'per' => 'admin#per', :via => :get
match 'admin' => 'admin#index', :via => :get
match 'admin/index' => 'admin#index', :via => :get
match 'admin/per' => 'admin#per', :via => :get
match 'admin/po' => 'admin#po', :via => :get
devise_for :users, :controllers => { :omniauth_callbacks => "callbacks" }
root 'home#index'
I have three templates: application, admin and home
I overwrite the default route after log in
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
#before_action :authenticate_user!
def after_sign_in_path_for(resource)
#request.env['omniauth.origin'] || stored_location_for(resource) || admin_path
admin_path
end
end
My last gem installed:
gem 'bootstrap-sass'
You need to run the generator for Devise views which will copy the necessary files in your views folder:
Run:
rails g devise:views
There is more information on configuring the Devise views here
Your problem probably isn't with Devise, it looks systemic to me.
#config/routes.rb
namespace :admin do
root "application#index" #->
resources :model_controller, path: "", only: :index do #-> url.com/admin/...
collection do
get :po #-> you shouldn't really have this
get :per #-> you shouldn't really have this
end
end
end
devise_for :users, :controllers => { :omniauth_callbacks => "callbacks" }
This will give you the following:
#app/controllers/admin/application_controller.rb
class Admin::ApplicationController < ApplicationController
before_action :authenticate_user!
def index
# do something here
end
end
This gives you the ability to create a custom "dashboard" type page for your admin area, from which you'll be able to use controllers bound to models.
Your po and per actions really shouldn't be there - they are not part of the CRUD system
In regards to your Devise views, the other answers are correct in that you would be best to generate the Devise views in your app:
rails generate devise:views
This won't solve your problem (hence why I downvoted the other answers). It will simply put the views in your app. It will do nothing apart from put code in a different place.
You will need to debug the issue:
Check the action you're seeing at /users/sign_in
Check the code in the <body> tags (which you haven't shown)
If the HTML is there, there will be some other issue preventing it from loading
If there is no HTML, it will likely mean a problem with the core of Devise
What I would recommend you do is the following:
Generate your views
From your screenshot, show us the contents of the <body> tag
Screenshot your console log (this will show any errors)
Update your question with the above
This will give you a much clearer perspective on what the potential issue will be, and allow other community members to better define the solution.

Redirecting to the devise controller after successfull login

I am new in Ruby On Rails. I am making an application using devise gem. My requirement is after logged in successfully I should redirect to devise controller again.
I have created devise controller as 'Users'
I have created one more controller home_controller.rb for redirecting
under home controller I have coded this.
def index
if user_signed_in?
redirect_to :controller => 'users', :action =>add
end
end
I have written add method under users_controller.rb
And under routes.rb I have coded this
devise_for :users, controllers:{sessions: "users/sessions"}
root :to => 'home#index'
match 'users/:action' => 'users#add', :as => :add
But its not redirecting. What should I do. any help. Thanks
Try this:-
resources :users do
member do
get "add"
end
end
If I understand correctly, you'll want to use the Devise redirect helpers:
#app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
def after_sign_in_path_for(resource)
user_add_path
end
end
I don't understand is this:
I should redirect to devise controller again
Devise controllers are split into SessionsController, RegistrationsController, PasswordsController, ConfirmationsController & UnlocksController.
Which one would you like to redirect to?
My hunch, and this is strengthened after reading your comments, is you want to redirect to the UsersController, like this:
#config/routes.rb
resources :users, only: :show do
get :add
end
#app/controllers/users_controller.rb
class UsersController < ApplicationController
def add
//declarations here
end
def show
//declarations here
end
end
This should help you

Resources