gpt4 book ai didi

ruby-on-rails - :only work in this ruby code?如何

转载 作者:太空宇宙 更新时间:2023-11-03 16:07:21 24 4
gpt4 key购买 nike

今天刚开始学ruby,对下面的代码有疑问。我想知道“:only =>:show”到底是如何影响这里的代码的。我假设如果我们没有“:only =>:show”,那么代码将直接处理身份验证。但是“:only =>:show”到底是做什么的呢?谢谢!

class PeopleController < ApplicationController

before_filter :authenticate, :only =>:show

def index
@people = ["luke-skywalker", "darth-vader"]
end

def show
@person = params[:id]
end

private

def authenticate
if params[:id] == "darth-vader"
redirect_to people_url, :notice => "The page is restricted."
#this is called the flash message
end
end
end

最佳答案

过滤器之前是一个回调,表示在处理您调用的实际操作之前调用此方法。 :only => :show 告诉回调只为 show Action 做那件事。如果取出来,则在所有 Action 之前调用该方法

关于ruby-on-rails - :only work in this ruby code?如何,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10825460/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com