简体   繁体   English

Ruby on Rails在应用程序控制器中访问参数哈希的内容

[英]Ruby on rails accessing content of params hash in application controller

I am trying to access the values stored in params hash from ApplicationController class directly.How can I do it? 我正在尝试直接从ApplicationController类访问存储在params哈希中的值,该怎么办? Foreaxmple: 前额:

 def setParams
 @parameters=params ???
 end

The univesal variable to get the ruby params is: 获得红宝石参数的univesal变量是:

params

This will return the hash of parameters from the request that you can see in the server log. 这将从您在服务器日志中看到的请求中返回参数的哈希值。

To access it you just need to use: 要访问它,您只需要使用:

def set_params #use ruby notation for methods which is underscore and _
 @parameters=params["key"]
end

Note that the key should be defined, as long as the incoming parameters for html view includes the codification: utf8, and the csrf token. 请注意,只要html视图的传入参数包括编码utf8和csrf令牌,就应该定义键。

If you want to access to the full request: 如果要访问完整的请求:

def set_params
  @request = request
end
class ApplicationController < ActionController::Base
  before_filter :handle_params

  def handle_params
    my_param_key = params[:my_param_key]
  end
end

Hope this helps someone starting to code on ruby! 希望这可以帮助某人开始在ruby上编码!

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

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