Since I am using Rspec to test whether my controller is working fine, I did things below:
in test_rspec.rb,
before do
params = {
date_begin: "2018-10-01",
date_end: "2018-10-05",
options: {country_code: "US"}
}
get :index, params: params, as: :json
end
it do
expected(response).to have_http_response(200) }
end
in test_controller.rb,
def index
puts params_checker
render json: Test.report(params_checker[:date_begin],
params_checker[:date_end],
params_checker[:options])
end
private
def params_checker
params.permit(:date_begin, :date_end, :options)
end
when I run code with rspec command, there goes something wrong, the parameter with nested hash :options is gone! below is the output of params in terminal & some of its error info:
> {"date_begin"=>"2018-10-01", "date_end"=>"2018-10-03"}
> ArgumentError:
wrong number of arguments (given 3, expected 2)
I search everywhere on internet and tried some of the solutions but it won`t help. I do know this error is caused by parameter missing. Could any one help me finger it out why hashes cannot be passed to controller`s params in my case?
note: I am using rails 5.1.6
options={country_code: "US"}tooptions: {country_code: "US"}2)Test.reportreceives only 2 arguments, you are passing 3options: {country_code: "US"}.to_sand in controllereval(params[:options])it worked fine. but this is not a really good way to solve the problem.params.permit(:date_begin, :date_end, options: [:country_code])and without.to_s