简体   繁体   English

从 Ruby on Rails 中的 ActionController::Parameters 中获取密钥

[英]Grabbing the key from the ActionController::Parameters in Ruby on Rails

This is my code这是我的代码

  def create
    @p_site = PSite.find(params.require(:plan_for).first[0])
    authorize @p_site, :c_s?
    if @p_site.save
      redirect_to :root, notice: "successfully added"
    else
      render :new
    end
  end

When I'm running it I get undefined method 'first' for <ActionController::Parameters:0x00005623c4d56f68>当我运行它时,我得到undefined method 'first' for <ActionController::Parameters:0x00005623c4d56f68>

My params[:plan_for] looks like this: #<ActionController::Parameters {"82"=>"annual"} permitted: false>我的 params[:plan_for] 看起来像这样: #<ActionController::Parameters {"82"=>"annual"} permitted: false>

I need to get the value of 82 and find in PSite.我需要获取 82 的值并在 PSite 中找到。 How can i do this?我怎样才能做到这一点?

Params is a Hash here and not an Array. Params 在这里是Hash而不是数组。 The way a Hash is structured is in key value pairs . Hash 的结构方式是键值对 For example:例如:

my_hash = { key: "Some Value" }
mz_hash[:key] #=> "Some Value"

So the first thing I am wondering is why you have 82 as the key here?所以我想知道的第一件事是为什么你在这里使用 82 作为密钥? If its an id I would restructure that to be something like this: { id: 82, recurrence: "annual" }如果它是一个 id,我会将其重组为如下所示: { id: 82, recurrence: "annual" }

Specifically for your case:专门针对您的情况:

params[:plan_for]["82"]          #=> "annual"
params.require(:plan_for)["82"]  #=> "annual"

# OR very dirty
params[:plan_for].values.first   #=> "annual"
params[:plan_for].keys.first     #=> "82"

You might see how the first two lines above wont work unless "82" is the key for all your params (in which case name it better.) and the latter might be a dirty solution for your code right now but also not something I would recommend.你可能会看到上面的前两行是如何工作的,除非“82”是你所有参数的键(在这种情况下命名它更好。)而后者现在可能是你代码的一个肮脏的解决方案,但我也不会这样做推荐。

Check out the documentation for ruby hashes查看ruby 哈希值的文档

I have first permitted my params and then converted it to hash and accessed it by using first[0].我首先允许我的参数,然后将其转换为 hash 并使用 first[0] 访问它。

@pp = params.require(:plan_for).permit..to_h @pp = params.require(:plan_for).permit..to_h

@p_site = PractitionerSite.find(@pp.first[0]) @p_site = PractitionerSite.find(@pp.first[0])

this will grab my value 82这将抢走我的价值 82

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

相关问题 合并ActionController :: Parameters与rails 5 - Merge ActionController::Parameters with rails 5 Rails ActionController参数错误 - Rails ActionController parameters Error ActionController :: Parameters重复键 - ActionController::Parameters duplicated key 在Ruby on Rails中的ActionController :: InvalidAuthenticityToken - ActionController::InvalidAuthenticityToken in ruby on rails Rails-字符串的ActionController :: Parameters - Rails - ActionController::Parameters to string Ruby 2 关键字 Arguments 和 ActionController::Parameters - Ruby 2 Keyword Arguments and ActionController::Parameters Ruby on Rails从ActionController插件模块中调用ActiveRecord模型 - Ruby on Rails calling ActiveRecord model from within ActionController plugin module 可以在rails上的ruby中的object.to_yaml(YAML)中删除“!ruby / hash:ActionController :: Parameters”吗? - Can “!ruby/hash:ActionController::Parameters” be removed when object.to_yaml (YAML) in ruby on rails? Ruby on Rails 6:表单通过隐藏字段提交散列数组但被阻止<ActionController::Parameters ..... permitted: false> - Ruby on Rails 6: form submits an array of hashes via hidden field but gets blocked by <ActionController::Parameters ..... permitted: false> Ruby:表单未提交-ActionController ::允许的参数:false - Ruby: Form not submit - ActionController::Parameters permitted: false
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM