简体   繁体   English

Rails登陆页面路由和设计

[英]Rails landing page routing and devise

My website should works just like facebook.com. 我的网站应该像facebook.com一样工作。 If the user is logged and if it goes to "/" it should be rendered home controller. 如果用户被记录并且如果它变为“/”则应该呈现为家庭控制器。 If it isn't logged it should be render landing_page controller 如果没有记录,则应该渲染landing_page控制器

"/ " && user_signed_in? “/ ”&& user_signed_in? ---> home controller ---> 家庭控制器

"/" && user_not_logged ---> landing_page controller “/” && user_not_logged ---> landing_page控制器

I'm using Rails 4 and Devise 我正在使用Rails 4和Devise

ApplicationController ApplicationController的

class ApplicationController < ActionController::Base

  before_filter :authenticate_user!
end

Routes.rb 的routes.rb

get "landing_page/index"

root 'home#index', :as => :home

How I could keep a "before_filter" in ApplicationControl that run in every controllers except "landing_page" controller? 如何在ApplicationControl中保留一个“before_filter”,除了“landing_page”控制器之外,它在每个控制器中运行?

Update 更新

If I go to "/en/landing_page" it render landing_page controller correctly (logged out), but if I go to "/" it redirect me to "/users/sign_in" 如果我转到“/ en / landing_page”它会正确渲染landing_page控制器(已注销),但如果我转到“/”它会将我重定向到“/ users / sign_in”

class LandingPageController < ApplicationController
  skip_before_action :authenticate_user!

  def index
  end

end

class ApplicationController < ActionController::Base

  before_action :authenticate_user!

end

Routes.rb 的routes.rb

 root 'landing_page#index'

SOLVED! 解决了!

LandingPageController LandingPageController

class LandingPageController < ApplicationController
  skip_before_action :authenticate_user!

  def index
  end

end

HomeController HomeController的

class HomeController < ApplicationController
  skip_before_action :authenticate_user!
  before_action :check_auth

def check_auth
    unless user_signed_in?
        redirect_to :controller => :landing_page
    end
end
 end 

ApplicationController ApplicationController的

class ApplicationController < ActionController::Base

  before_action :authenticate_user!

end

Routes.rb 的routes.rb

 root 'landing_page#index'

I think you could easily add a before filter to handle this action. 我认为你可以轻松添加一个前置过滤器来处理这个动作。

Like in this answer 喜欢这个答案

I think you can write confirm_logged_in function action in controller 我想你可以在controller中编写confirm_logged_in函数动作

before_filter :confirm_logged_in before_filter:confirm_logged_in

In that function you can mention how you want to render pages based on logged in user 在该功能中,您可以提及您希望如何根据登录用户呈现页面

def confirm_logged_in<br>
  unless session[:user_id]
  flash[:notice] = 'Please log in.'
  redirect_to(:controller => 'access', :action => 'login')
  return false #halts the before_filter<

else
return true
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM