Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Rails slice() helper incompatibility in Rails 2.1.1

#1096

String.slice used to return the same types of keys as the original hash. As of Rails 2.1.1, however, it always returns stringified keys.

You can see this behavior in a snipped like the following:

user_params = params.slice(:username, :password)

In Rails 2.1.0 and earlier, you could then say:

user_params[:username] user_params[:password]

However, in Rails 2.1.1, you can only get to them stringified:

user_params['username'] user_params['password']

Attempting to use the symbols yields nil values.

Reported by Nate Wiger · September 23rd, 2008 @ 12:04 AM

State: committed
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. Michael Johann
    Michael Johann

    I'm not able to find the issue. Everything works fine with this testcase:

    def test_slice_access_with_symbols

    original = { :username => 'motu', :password => 'motupass', :email => 'myemail@email.domain'}
    
    original.slice!(:username, :password)
    
    username = original[:username]
    assert_equal username, "motu"
    
    

    end

    September 23rd, 2008 @ 09:36 AM

  2. Adam Cigánek
    Adam Cigánek

    I guess the problem is that params is HashWithIndifferentAccess but slice gives back just plain Hash. Perhaps slice should give return of the same type as it's receiver?

    September 23rd, 2008 @ 09:53 AM

  3. Michael Johann
    Michael Johann

    Using the method @@@ ruby with_indifferent_access@@@ on the Hash converts the hash into an object of type @@@ruby HashWithIndifferentAccess@@@. So the following test case should fail, of there is an issue, I guess. I'm still not able to reproduce the problem. Maybe I on the wrong way.

    
      def test_indifferent_slice_access_with_symbols
        original = { :username => 'motu', :password => 'motupass', :email => 'myemail@email.domain'}.with_indifferent_access
    
        original.slice!(:username, :password).with_indifferent_access
    
        username = original[:username]
        assert_equal username, "motu"
    
      end
    
    

    September 23rd, 2008 @ 10:10 AM

  4. Adam Cigánek
  5. Michael Johann
    Michael Johann

    Now I understand the problem. Thank you for cleaning my eyes. ;-)

    Let's see...

    September 23rd, 2008 @ 11:09 AM

  6. Adam Cigánek
  7. Michael Johann
    Michael Johann
    • Tag changed from 2.1, bug, core_ext, helper to 2.1, bug, core_ext, helper, patch

    Great. Works. :-)

    September 23rd, 2008 @ 11:53 AM

  8. Michael Koziarski
    Michael Koziarski

    Looks good,

    Backported and applied

    September 23rd, 2008 @ 01:07 PM

  9. Repository
    Repository
    • State changed from new to committed

    (from [3b9c2fdf9e1813b8317d529aeb4074806e14ade2]) slice now returns indifferent hash if called on one

    Signed-off-by: Michael Koziarski michael@koziarski.com

    [#1096 Rails slice() helper incompatibility in Rails 2.1.1 state:committed]

    Conflicts:

    activesupport/lib/active_support/core_ext/hash/slice.rb
    
    

    http://github.com/rails/rails/co...

    September 23rd, 2008 @ 01:08 PM

  10. Repository
    Repository

    (from [2e75bd0808f4dcac328b690aaad176cbfe96773e]) slice now returns indifferent hash if called on one

    Signed-off-by: Michael Koziarski michael@koziarski.com

    [#1096 Rails slice() helper incompatibility in Rails 2.1.1 state:committed] http://github.com/rails/rails/co...

    September 23rd, 2008 @ 01:08 PM