本文介绍了如何在ruby中覆盖[]括号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <%= collection_select(作业,清除,@allClearances,清除,清除,{:提示=>选择清除})%> ;然后,Ruby将使用 id =Jobs_clearance创建一个HTML选择标记



name =Jobs [clearance]



我想将参数发送到我的控制器,如下所示:

 类JobsController< ApplicationController 
def foo
@clearance = params [:Jobs [clearance]]
end

不幸的是,Ruby只读取:Jobs作为符号,而不是:Jobs [clearance]

有没有办法逃避 [] 的?反斜杠不起作用

解决方案

您需要使用 params [:Jobs] [:clearance]



params 是所有请求参数的哈希值。但是 params [:Jobs ]也是全部的哈希:作业参数。所以调用 params [:Jobs] [:清除] 正在调用 [] > params [:Jobs] object passing :clearance in作为参数。


I am writing an Ajax request form with Ruby on Rails using a collection_select tag that looks like this:

<%= collection_select("Jobs", "clearance", @allClearances, "clearance", "clearance", {:prompt => "Select a Clearance"} )%>

Ruby then builds an HTML select tag with id = "Jobs_clearance" and name = "Jobs[clearance]"

I want to send the parameter to my controller, which looks like this:

class JobsController < ApplicationController
  def foo
    @clearance = params[:Jobs[clearance]]
  end

Unfortunately, Ruby only reads ":Jobs" as the symbol instead of ":Jobs[clearance]"
Is there a way to escape the []'s? backslash isn't working.

解决方案

You need to use params[:Jobs][:clearance]

params is a hash of all the request parameters. But params[:Jobs] is ALSO a hash of all :Jobs parameters. So calling params[:Jobs][:clearance] is calling the [] method on the params[:Jobs] object passing :clearance in as a parameter.

这篇关于如何在ruby中覆盖[]括号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 22:21