简体   繁体   English

使用before_action的rails application_controller未处理

[英]rails application_controller using before_action is not processed

I wan't to use the before_action in application_controller.rb and then some skip_before_actions ... 我不想在application_controller.rb中使用before_action,然后再使用skip_before_actions ...

bit the defined function is not called ... 位定义的函数不被调用...

application_controller.rb application_controller.rb

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  layout "application"

  before_action :user_logged_in, :set_locale

  private

  def set_locale
     I18n.locale = params[:locale] || I18n.default_locale
  end

  # Prüft ob ein Nutzer eingeloggt ist.
  def user_logged_in

    puts "HA"

   if session[:user_id].nil?
     flash[:error] = "error"
      redirect_to :controller => :startsites, :action => :index
   else
      flash[:error] = "ok"
      redirect_to :controller => :startsites, :action => :index
   end

  end
end

The puts "HA" in user_logged_in is not shown ... 没有显示user_logged_in中的puts“ HA” ...

and in some controllers I tried to use this: 在某些控制器中,我尝试使用此功能:

class MoviesController < ActionController::Base
   skip_before_action :user_logged_in, only: [:index, :show]

also not working ... why? 也不能正常工作...为什么?

Where are you looking for the puts ? 您在哪里寻找puts If you were to call the Rails logger instead, it would work. 如果您改为调用Rails logger ,则可以使用。

Try to insert a binding.pry instead of the puts and see what happens (you obviously need to have pry installed on your machine) 尝试插入binding.pry而不是puts,看看会发生什么(您显然需要在计算机上安装pry

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

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