开发者

Devise: Sign Up Page as Welcome/Landing Page then to User Profile

Using devise, how do i make my Sign Up as my landing/welcome page and then after sign up they go inside the site to the Profile/signed in area?

I am trying to figure out how to make it like where Facebook their is two sides of the websit开发者_运维问答e; the Outside (sign up, about,contact,etc) and The Inside (Profile,sign out,etc) for Users only after sign up or sign in.

Thank you.

P.S. I am new at Ruby on Rails and creating applications but i did do the authentication system with the Rails 3 Tutorial, i understand most things to start using Devise, i jst dont know where to start with this situation.

I was going to use 2 application layouts, 1 before sign up which is layouts/welcome.html.erb with PagesController (about,terms,etc) and the other for signed in users which will be layouts/application.html.erb with ApplicationController (profile,news,add,etc), is this the best steps?


in your routes.rb:

root :to => 'welcome#index'

Where welcome is the controller and index is the action.

In your application controller:

def after_sign_in_path_for(user)
   "/url_you_want_to_redirect_to/"
end


This my new and updated way using Rails 3.1.0 and Devise 1.5.0:

routes.rb

root :to => "pages#redirect_to_sign_up"

devise_for :users do
  get "welcome" => "devise/registrations#new", :as => :new_user_registration
  get "account_settings" => "devise/registrations#edit"
  get "sign_in" => "devise/sessions#new"
  get "sign_out" => "devise/sessions#destroy"
  get "new_password", :to => "devise/passwords#new"
end

match 'home',      :to => "user_pages#home"

namespace :user do
  root :to => "user_pages#home"
end

application_controller.rb

class ApplicationController < ActionController::Base
  protect_from_forgery

  protected

  def after_sign_in_path_for(resource)
    stored_location_for(:user) || root_path
  end

  private

  def after_sign_out_path_for(resource)
    stored_location_for(:user) || root_path
  end
end

pages_controller.rb

class PagesController < ApplicationController
  def redirect_to_sign_up
    if signed_in?.blank?
      redirect_to new_user_registration_path
    else
      redirect_to home_path
    end
  end
end

user_pages_controller.rb

class UserPagesController < ApplicationController
  before_filter :authenticate_user!

  def home
  end

  def profile
  end
end


I find it easiest to root to the desired authenticated landing page and just use a before_filter to force the user to sign in/sign up first via a before_filter.

In this case, let's say your "signed in area" is a controller/action called profile/index.

In your routes.rb, set the root to profile/index.

root :to => 'profile#index'

Then in your profile_controller, set the following before_filter.

before_filter :authenticate_user!

This will automatically push a logged in user (or one that logged in earlier and set a Remember Me cookie) straight to the profile page. Any unauthenticated users will automatically end up on Sign In. You'll want a link (or separate tab) on that page to Sign Up as well.


On the root page check to see if the user is signed in, and redirect based on that.

redirect_to sign_up_path if current_user.nil?

Alternatively, you could render different templates instead of a redirect, but I think it's cleaner to have a 1:1 mapping between urls and pages.


Another approach is to modify devise. Edit devise_gem_path/lib/devise/failure_app.rb and replace the two occurrences in the redirect_url method of:

:"new_#{scope}_session_path"

with:

:"new_#{scope}_registration_path"

A less destructive solution would make redirect_url's response more configurable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜