public
Created

Re: [Rails] Parameter with the same name as the controller returning the params hash instead of nil. I tried to reproduce the problem, but I am unable to.

  • Download Gist
log
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
12:08:04 - INFO - Running: spec/controllers/shout_controller_spec.rb
12:08:08 test_log.1 | Connecting to database specified by database.yml
12:08:09 guard.1 |
12:08:09 guard.1 | ShoutController
12:08:09 guard.1 | POST 'create'
12:08:09 test_log.1 | (0.3ms) begin transaction
12:08:09 test_log.1 | Processing by ShoutController#create as HTML
12:08:09 test_log.1 | Parameters: {"api_key"=>"aaa", "authentication_token"=>"bbbb", "shout"=>"GONZO!"}
12:08:09 test_log.1 | /Users/tamara/Projects/rubystuff/railsapps/my_newer_app/app/controllers/shout_controller.rb:3:in `create':
12:08:09 test_log.1 | Params: {"api_key"=>"aaa", "authentication_token"=>"bbbb", "shout"=>"GONZO!", "controller"=>"shout", "action"=>"create"}
12:08:09 test_log.1 | Shout in params? true
12:08:09 test_log.1 | params[:shout] GONZO!
12:08:09 test_log.1 | Completed 200 OK in 1ms (Views: 0.4ms | ActiveRecord: 0.0ms)
12:08:09 guard.1 | {"errors"=>"Done", "msg"=>"GONZO!", "params"=>{"api_key"=>"aaa", "authentication_token"=>"bbbb", "shout"=>"GONZO!", "controller"=>"shout", "action"=>"create"}}
12:08:09 test_log.1 | (0.1ms) rollback transaction
12:08:09 guard.1 | should return successful with :shout
12:08:09 test_log.1 | (0.1ms) begin transaction
12:08:09 test_log.1 | Processing by ShoutController#create as HTML
12:08:09 test_log.1 | Parameters: {"api_key"=>"aaa", "authentication_token"=>"bbbb"}
12:08:09 test_log.1 | /Users/tamara/Projects/rubystuff/railsapps/my_newer_app/app/controllers/shout_controller.rb:3:in `create':
12:08:09 test_log.1 | Params: {"api_key"=>"aaa", "authentication_token"=>"bbbb", "controller"=>"shout", "action"=>"create"}
12:08:09 test_log.1 | Shout in params? false
12:08:09 test_log.1 | params[:shout]
12:08:09 test_log.1 | we did not get the shout!
12:08:09 test_log.1 | Completed 400 Bad Request in 1ms (Views: 0.4ms | ActiveRecord: 0.0ms)
12:08:09 guard.1 | {"errors"=>"we did not get the shout!", "params"=>{"api_key"=>"aaa", "authentication_token"=>"bbbb", "controller"=>"shout", "action"=>"create"}}
12:08:09 test_log.1 | (0.1ms) rollback transaction
12:08:09 guard.1 | should return failure without :shout
12:08:09 guard.1 |
12:08:09 guard.1 | Finished in 0.02566 seconds
12:08:09 guard.1 | 2 examples, 0 failures
12:08:09 guard.1 |
shout_controller.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
class ShoutController < ApplicationController
def create
Rails.logger.debug [caller(0,1).first + ?:,
"Params: #{params}",
"Shout in params? #{params.key? :shout}",
"params[:shout] #{params[:shout]}"
].join("\n ")
unless params.key? :shout
Rails.logger.debug " we did not get the shout!"
render :json => { :errors => "we did not get the shout!", :params => params }, :status => :bad_request
return
end
shout = params[:shout]
render :json => { :errors => 'Done', :msg => shout, :params => params }, :status => :ok
end
end
shout_controller_spec.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
require 'spec_helper'
 
describe ShoutController do
 
describe "POST 'create'" do
it "should return successful with :shout" do
attr = {"api_key" => "aaa",
"authentication_token" => "bbbb",
"shout" => "GONZO!"}
post 'create', attr
parsed_body = JSON.parse(response.body)
p parsed_body
response.should be_success
parsed_body["params"].should have_key("shout")
parsed_body["msg"].should == "GONZO!"
end
it "should return failure without :shout" do
attr = {"api_key" => "aaa",
"authentication_token" => "bbbb"}
post 'create', attr
parsed_body = JSON.parse(response.body)
p parsed_body
response.should_not be_success
parsed_body["params"].should_not have_key("shout")
parsed_body["errors"].should_not be_empty
end
 
end
 
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.