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]paramsis a hash of all the request parameters. Butparams[:Jobs] is ALSO a hash of all :Jobs parameters. So callingparams[:Jobs][:clearance]is calling the[]method on theparams[:Jobs]object passing:clearancein as a parameter.