Skip to content

Instantly share code, notes, and snippets.

@CoryFoy
Last active January 12, 2016 04:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CoryFoy/6ac564b7cce4f96db9b2 to your computer and use it in GitHub Desktop.
Save CoryFoy/6ac564b7cce4f96db9b2 to your computer and use it in GitHub Desktop.
Rails Bug(?) where an object served through the controller as json modifies the URL parameter
This is a bug where a model with a `url` attribute gets it modified when a controller is returning a JSON response
as_json response: [{"id"=>1, "title"=>"Hello 1", "url"=>"http://www.hello.com", "created_at"=>Tue, 12 Jan 2016 04:12:22 UTC +00:00, "updated_at"=>Tue, 12 Jan 2016 04:12:22 UTC +00:00}, {"id"=>2, "title"=>"Hello 2", "url"=>"http://www.hello.com", "created_at"=>Tue, 12 Jan 2016 04:12:29 UTC +00:00, "updated_at"=>Tue, 12 Jan 2016 04:12:29 UTC +00:00}]
JSON response in browser: [{"id":1,"title":"Hello 1","url":"http://localhost:3000/stories/1.json"},{"id":2,"title":"Hello 2","url":"http://localhost:3000/stories/2.json"}]
Corys-MacBook-Pro-2:Workspace foyc$ rails --version
Rails 4.2.5
Corys-MacBook-Pro-2:Workspace foyc$ rails new rails-bug
create
create README.rdoc
create Rakefile
create config.ru
create .gitignore
create Gemfile
create app
create app/assets/javascripts/application.js
create app/assets/stylesheets/application.css
create app/controllers/application_controller.rb
create app/helpers/application_helper.rb
create app/views/layouts/application.html.erb
create app/assets/images/.keep
create app/mailers/.keep
create app/models/.keep
create app/controllers/concerns/.keep
create app/models/concerns/.keep
create bin
create bin/bundle
create bin/rails
create bin/rake
create bin/setup
create config
create config/routes.rb
create config/application.rb
create config/environment.rb
create config/secrets.yml
create config/environments
create config/environments/development.rb
create config/environments/production.rb
create config/environments/test.rb
create config/initializers
create config/initializers/assets.rb
create config/initializers/backtrace_silencers.rb
create config/initializers/cookies_serializer.rb
create config/initializers/filter_parameter_logging.rb
create config/initializers/inflections.rb
create config/initializers/mime_types.rb
create config/initializers/session_store.rb
create config/initializers/wrap_parameters.rb
create config/locales
create config/locales/en.yml
create config/boot.rb
create config/database.yml
create db
create db/seeds.rb
create lib
create lib/tasks
create lib/tasks/.keep
create lib/assets
create lib/assets/.keep
create log
create log/.keep
create public
create public/404.html
create public/422.html
create public/500.html
create public/favicon.ico
create public/robots.txt
create test/fixtures
create test/fixtures/.keep
create test/controllers
create test/controllers/.keep
create test/mailers
create test/mailers/.keep
create test/models
create test/models/.keep
create test/helpers
create test/helpers/.keep
create test/integration
create test/integration/.keep
create test/test_helper.rb
create tmp/cache
create tmp/cache/assets
create vendor/assets/javascripts
create vendor/assets/javascripts/.keep
create vendor/assets/stylesheets
create vendor/assets/stylesheets/.keep
run bundle install
Fetching gem metadata from https://rubygems.org/...........
Fetching version metadata from https://rubygems.org/...
Fetching dependency metadata from https://rubygems.org/..
Resolving dependencies.....
Using rake 10.4.2
Using i18n 0.7.0
Using json 1.8.3
Using minitest 5.8.3
Using thread_safe 0.3.5
Using builder 3.2.2
Using erubis 2.7.0
Using mini_portile2 2.0.0
Using rack 1.6.4
Using mime-types 2.99
Using arel 6.0.3
Using debug_inspector 0.0.2
Using bundler 1.11.2
Using byebug 8.2.1
Using coffee-script-source 1.10.0
Using execjs 2.6.0
Using thor 0.19.1
Using concurrent-ruby 1.0.0
Using multi_json 1.11.2
Using sass 3.4.21
Using tilt 2.0.2
Using spring 1.6.2
Using sqlite3 1.3.11
Using rdoc 4.2.1
Using tzinfo 1.2.2
Using nokogiri 1.6.7.1
Using rack-test 0.6.3
Using mail 2.6.3
Using binding_of_caller 0.7.2
Using coffee-script 2.4.1
Using uglifier 2.7.2
Using sprockets 3.5.2
Using sdoc 0.4.1
Using activesupport 4.2.5
Using loofah 2.0.3
Using rails-deprecated_sanitizer 1.0.3
Using globalid 0.3.6
Using activemodel 4.2.5
Using jbuilder 2.4.0
Using rails-html-sanitizer 1.0.2
Using rails-dom-testing 1.0.7
Using activejob 4.2.5
Using activerecord 4.2.5
Using actionview 4.2.5
Using actionpack 4.2.5
Using actionmailer 4.2.5
Using railties 4.2.5
Using sprockets-rails 3.0.0
Using coffee-rails 4.1.1
Using jquery-rails 4.0.5
Using rails 4.2.5
Using sass-rails 5.0.4
Using web-console 2.2.1
Using turbolinks 2.5.3
Bundle complete! 12 Gemfile dependencies, 54 gems now installed.
Use `bundle show [gemname]` to see where a bundled gem is installed.
run bundle exec spring binstub --all
* bin/rake: spring inserted
* bin/rails: spring inserted
Corys-MacBook-Pro-2:Workspace foyc$ cd rails-bug/
Corys-MacBook-Pro-2:rails-bug foyc$ rails generate scaffold Story title:string url:string
Running via Spring preloader in process 46579
invoke active_record
create db/migrate/20160112041138_create_stories.rb
create app/models/story.rb
invoke test_unit
create test/models/story_test.rb
create test/fixtures/stories.yml
invoke resource_route
route resources :stories
invoke scaffold_controller
create app/controllers/stories_controller.rb
invoke erb
create app/views/stories
create app/views/stories/index.html.erb
create app/views/stories/edit.html.erb
create app/views/stories/show.html.erb
create app/views/stories/new.html.erb
create app/views/stories/_form.html.erb
invoke test_unit
create test/controllers/stories_controller_test.rb
invoke helper
create app/helpers/stories_helper.rb
invoke test_unit
invoke jbuilder
create app/views/stories/index.json.jbuilder
create app/views/stories/show.json.jbuilder
invoke assets
invoke coffee
create app/assets/javascripts/stories.coffee
invoke scss
create app/assets/stylesheets/stories.scss
invoke scss
create app/assets/stylesheets/scaffolds.scss
Corys-MacBook-Pro-2:rails-bug foyc$ rake db:migrate
== 20160112041138 CreateStories: migrating ====================================
-- create_table(:stories)
-> 0.0013s
== 20160112041138 CreateStories: migrated (0.0014s) ===========================
Corys-MacBook-Pro-2:rails-bug foyc$ rails c
Running via Spring preloader in process 46586
Loading development environment (Rails 4.2.5)
~> Console extensions: wirble hirb ap rails2 rails3 pm interactive_editor
>> Story.create(title: "Hello 1", url: "http://www.hello.com")
SCHEMA (1.5ms) PRAGMA table_info("stories")
SCHEMA (0.2ms) SELECT name FROM sqlite_master WHERE (type = 'table' OR type = 'view') AND NOT name = 'sqlite_sequence'
SCHEMA (0.1ms) PRAGMA table_info("stories")
(0.1ms) begin transaction
(0.2ms) begin transaction
SQL (0.4ms) INSERT INTO "stories" ("title", "url", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["title", "Hello 1"], ["url", "http://www.hello.com"], ["created_at", "2016-01-12 04:12:22.882378"], ["updated_at", "2016-01-12 04:12:22.882378"]]
SQL (0.6ms) INSERT INTO "stories" ("title", "url", "created_at", "updated_at") VALUES (?, ?, ?, ?)
(1.9ms) commit transaction
(2.1ms) commit transaction
=> #<Story id: 1, title: "Hello 1", url: "http://www.hello.com", created_at: "2016-01-12 04:12:22", updated_at: "2016-01-12 04:12:22">
>> Story.create(title: "Hello 2", url: "http://www.hello.com")
(0.2ms) begin transaction
(0.3ms) begin transaction
SQL (0.5ms) INSERT INTO "stories" ("title", "url", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["title", "Hello 2"], ["url", "http://www.hello.com"], ["created_at", "2016-01-12 04:12:29.704032"], ["updated_at", "2016-01-12 04:12:29.704032"]]
SQL (0.7ms) INSERT INTO "stories" ("title", "url", "created_at", "updated_at") VALUES (?, ?, ?, ?)
(6.2ms) commit transaction
(6.3ms) commit transaction
=> #<Story id: 2, title: "Hello 2", url: "http://www.hello.com", created_at: "2016-01-12 04:12:29", updated_at: "2016-01-12 04:12:29">
>> Story.all.as_json
Story Load (0.4ms) SELECT "stories".* FROM "stories"
Story Load (0.5ms) SELECT "stories".* FROM "stories"
=> [{"id"=>1, "title"=>"Hello 1", "url"=>"http://www.hello.com", "created_at"=>Tue, 12 Jan 2016 04:12:22 UTC +00:00, "updated_at"=>Tue, 12 Jan 2016 04:12:22 UTC +00:00}, {"id"=>2, "title"=>"Hello 2", "url"=>"http://www.hello.com", "created_at"=>Tue, 12 Jan 2016 04:12:29 UTC +00:00, "updated_at"=>Tue, 12 Jan 2016 04:12:29 UTC +00:00}]
>> exit
Corys-MacBook-Pro-2:rails-bug foyc$ rails s
=> Booting WEBrick
=> Rails 4.2.5 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2016-01-11 22:12:47] INFO WEBrick 1.3.1
[2016-01-11 22:12:47] INFO ruby 2.2.1 (2015-02-26) [x86_64-darwin14]
[2016-01-11 22:12:47] INFO WEBrick::HTTPServer#start: pid=46587 port=3000
Started GET "/stories.json" for ::1 at 2016-01-11 22:12:57 -0600
ActiveRecord::SchemaMigration Load (0.6ms) SELECT "schema_migrations".* FROM "schema_migrations"
Processing by StoriesController#index as JSON
Story Load (0.5ms) SELECT "stories".* FROM "stories"
Rendered stories/index.json.jbuilder (10.0ms)
Completed 200 OK in 37ms (Views: 25.4ms | ActiveRecord: 0.9ms)
Actual Output on screen: [{"id":1,"title":"Hello 1","url":"http://localhost:3000/stories/1.json"},{"id":2,"title":"Hello 2","url":"http://localhost:3000/stories/2.json"}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment