2016-03-15 53 views
0

我有一个包含用户的rails站点,并且这些用户可以创建特定页面。Rails控制器:仅对某些操作进行身份验证

我想让这些页面可以看到没有登录的人。所以,我正在为该页面制作另一个视图&控制器操作。

目前,我唯一的问题是使一个控制器有一个操作需要身份验证,另一个操作不会,而保留在同一个控制器上。

我有什么

class PageController < ApplicationController 
    before_action :authenticate_user! 
    skip_before_filter :verify_authenticity_token 
    respond_to :json 


    def show 
     @page = Page.new 
     .... 
    end 
    .... 
end 

我想要什么

class PageController < ApplicationController 
    if params[:action] != 'show_public' 
     before_action :authenticate_user! 
     skip_before_filter :verify_authenticity_token 
    end 
    respond_to :json 



    def show 
     @page = Page.new 
     @current_user = current_user 
     .... 
    end 

    def show_public 
     @page = Page.new 
     .... 
    end 
    .... 
end 

我目前得到的是错误:

undefined local variable or method `params' for PagesController:Class 

其他人为本网站提供了大部分的代码(并且从此离开),而我是rails新手。所以,如果我正在用完全错误的方法解决这个问题,请让我知道。

回答

2

你可以这样说:

before_action :authenticate_user!, only: [:show] 

因此,这将只在show操作来运行。有时你需要在所有运行,并留下一个如此:

before_action :authenticate_user!, except: [:show] 

所以这不会对show运行,但会在所有其他操作运行。而假设你before_action是在应用控制器,你需要在继承的控制器跳过你可以做这样的:

skip_before_action :require_login, only: [:show] 
+0

谢谢你,是有办法的一切,但show_public认证? – Rorschach

+2

',当然除外:[:show_public]'。 –