0

I recently implemented a new homepage (index.html) on my rails app (into folder public/index.html) with /signin and /signup paths. Before I implemented index.html on my website, I have been redirecting users to root_url (which is home.html.erb) after sign in or sign up. When they sign out, I've been redirecting them to root_path.

The problem is that after this new index.html, when users try to log in or sign up, they get redirected back to index.html. Is there a place to have them log in successfully to the original root_url without requiring many code edits?

class SessionsController < ApplicationController

  def new
  end

  def create
    user = User.find_by_email(params[:session][:email])
    if user && user.authenticate(params[:session][:password])
      sign_in user
      redirect_to root_path
    else
      flash.now[:error] = "Invalid email/password combination"
      render 'new'
    end
  end

  def destroy
    sign_out
    redirect_to root_path
  end
end

UsersController

def create
    @user = User.new(params[:user])
    if @user.save
      sign_in @user
      flash[:success] = "Welcome!"
      redirect_to root_path
    else
      render 'new'
    end
  end

This is what I have in my routes.rb

root to: 'static_pages#home'
5
  • Find the login/sign_up action in either your session_controller or users_controller and you will se a redirect action at the end of both. Instead of redirect_to whatever, change it to redirect_to root_path
    – Zippie
    Mar 17, 2013 at 22:07
  • I changed to root_path under def create in both controllers but I'm still getting the same issue. I posted up the codes in the updated post
    – johbones
    Mar 17, 2013 at 22:15
  • can you run rake routes and see if the home.html.erb is really the root_path
    – Zippie
    Mar 17, 2013 at 22:33
  • I ran rake routes and this is what I found: root / static_pages#home
    – johbones
    Mar 17, 2013 at 23:06
  • 1
    please read the two upvoted answers
    – Zippie
    Mar 17, 2013 at 23:07

4 Answers 4

1

First off the only difference between root_url and root_path (and in general between foo_url and foo_path) is that the former is a full url (i.e. http://example.com/...) whereas the latter is just the path (the bit after the hostname). For a simple redirect they'll have the same result.

If public/index.html exists then that is where visits to '/', (i.e. root_path) will go.

If you want users to be sent to a different page after signup then change your redirect. For example if your routes file had

get '/home' => 'home#index', :as => :home

then redirecting to home_path would send people to the index action of the home controller.

1

The problem seems to be that your public/index.html overrides your root_path Rails route.

You cannot access the root_path if there is a file called index.html in your public directory.

You need to rename index.html to something else or use another path other than your root_path

EDIT:

Another option is to have two different erb templates for the root_path. Then in the controller action for the root_path, you could do this:

class StaticPages < ApplicationController
  def home
    if user_signed_in?
      render 'home_signed_in'
    else
      render 'home_signed_out'
    end
  end
end

You would then need to create two erb templates at,

/app/views/static_pages/home_signed_in.html.erb

and

/app/views/static_pages/home_signed_out.html.erb

You would also need to define or replace the user_signed_in? method in my example code with your own method to detect if the user is signed in. And don't forget to remove /public/index.html

2
  • If I rename index.html to something else, how can I make this page come up when someone goes to www.mydomain.com?
    – johbones
    Mar 17, 2013 at 22:40
  • I'm not sure you can. You can only have index.html or root_path. The two conflict and your web server will choose the static index.html over the Rails root_path.
    – Sam
    Mar 17, 2013 at 22:49
0

Try to change root_path with home_ControllerName_path, where ControllerName must be the name of the controller handling the home action.

6
  • he is doing it already. The name of the controller is static_pages
    – Zippie
    Mar 17, 2013 at 22:34
  • @johbones Based on your code, it must be home_static_page_path
    – thesubroot
    Mar 17, 2013 at 22:37
  • I tried 'redirect_to home_static_page_path' in my controllers, but I get this error: undefined local variable or method `home_static_page_path' for #<SessionsController:0x4e83070>
    – johbones
    Mar 17, 2013 at 22:45
  • Try with this redirect_to :controller => 'static_pages', :action => 'home'
    – thesubroot
    Mar 17, 2013 at 22:55
  • unfortunately I think this may not work. This is the error that I got: AbstractController::DoubleRenderError in SessionsController (Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action.")
    – johbones
    Mar 17, 2013 at 23:02
0

go to your routes.rb file and make following change

root :to => 'main#home_page'

you can redirect to any route by modifying root: in config/routes.rb file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.