【问题标题】:Pass action to controller custom controller method - ROR将动作传递给控制器​​自定义控制器方法 - ROR
【发布时间】:2017-03-13 21:56:05
【问题描述】:

如何将用户正在执行的操作传递给控制器​​中的方法?

例如:

before_action :require_login, only: [:new, :create, :edit, :update, :destroy]

如果用户在登录我的方法之前尝试使用 :edit 操作,我想说这样的话:

def require_login 
  unless current_user
    if (:edit action)
      flash[:alert] = "You must log in before you are able to edit foo"
    end
  end
end

【问题讨论】:

    标签: ruby-on-rails controller parameter-passing action


    【解决方案1】:

    params[:action] 你在找什么?

    flash[:alert] = "You must log in before you are able to #{params[:action]} foo"
    

    params[:controller] 也应该可用

    【讨论】:

      猜你喜欢
      • 2014-04-07
      • 2012-03-19
      • 1970-01-01
      • 2013-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多