I'm trying to pass an array in the query string of an rspec test but it's giving me an error. I will be sending query strings like "products[]=desk,chair" and expect the controller to be able to handle it.
Here's the error:
Failure/Error: get :index, { :format => :json, :products => [product_1, product_2]}, { "Accept" => "application/json" }
NoMethodError:
       undefined method `each' for "[\"desk\", \"chair\"]":String
Here's my test method:
product_1 = "desk"
product_2 = "chair"
get :index, { :format => :json, :products => [product_1, product_2]}, { "Accept" => "application/json" }
Heres my controller:
products.each do |product|
    puts "product: #{product}"
end
def products
    params[:products].to_s
end
Any ideas?
Note: this is running Rails 3.2.12
You are defining products as params[:products].to_s that makes it an string, no matter what you send. So when you do products.each in the index method it fails.
Do the each on params[:products] instead:
def index
  params[:products].each do |product|
    puts "product: #{product}"
  end
end
I'm sure that puts, is just troubleshooting code, but you shouldn't use it on your controller.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With