Skip to content

Conversation

dianedelallee
Copy link
Contributor

The dig solution did not work on my machine, but I am not sure why (quite new with ruby and rails).

the solution with brackets wroks, so maybe we can change that, But any explanation is really welcomed 😄

The dig solution did not work on my machine, but I am not sure why (quite new with ruby and rails).

the solution with brackets wroks, so maybe we can change that, But any explanation is really welcomed 😄
@dianedelallee
Copy link
Contributor Author

If you agreed with the change, we should also change the example that use dig. I think there is 4 or 5 example that use this sample (for the factorisation)

@dianedelallee
Copy link
Contributor Author

oooohhh ok find the issue...
the test should be

test 'should show orders' do
    get api_v1_orders_url, headers: { Authorization: JsonWebToken.encode(user_id: @order.user_id) }, as: :json
    assert_response :success
    json_response = JSON.parse(response.body, symbolize_names: true)
    assert_not_nil json_response.dig(:links, :first)
    assert_not_nil json_response.dig(:links, :last)
    assert_not_nil json_response.dig(:links, :next)
    assert_not_nil json_response.dig(:links, :prev)
  end

, symbolize_names: true was missing i guess 🤔

@madeindjs
Copy link
Owner

I think symbolize_names is the right solution.

Here an example to reproduce error:

2.6.5 :001 > require 'json'
2.6.5 :002 > data = JSON.parse '{ "a": { "b": { "c" : 1 } } }'
 => {"a"=>{"b"=>{"c"=>1}}}
2.6.5 :003 > data.dig :a, :b, :c
 => nil 

using symbolize_names works

2.6.5 :004 > data = JSON.parse '{ "a": { "b": { "c" : 1 } } }', symbolize_names: true
 => {:a=>{:b=>{:c=>1}}} 
2.6.5 :005 > data.dig :a, :b, :c
 => 1

We should avoid using array because this may produce NoMethodError. Here an example

2.6.5 :006 > data[:x][:y][:z]
Traceback (most recent call last):
        7: from /home/alexandre/.rvm/gems/ruby-2.6.5/bin/ruby_executable_hooks:24:in `<main>'
        6: from /home/alexandre/.rvm/gems/ruby-2.6.5/bin/ruby_executable_hooks:24:in `eval'
        5: from /home/alexandre/.rvm/rubies/ruby-2.6.5/bin/irb:23:in `<main>'
        4: from /home/alexandre/.rvm/rubies/ruby-2.6.5/bin/irb:23:in `load'
        3: from /home/alexandre/.rvm/rubies/ruby-2.6.5/lib/ruby/gems/2.6.0/gems/irb-1.0.0/exe/irb:11:in `<top (required)>'
        2: from (irb):06
        1: from (irb):06:in `rescue in irb_binding'
NoMethodError (undefined method `[]' for nil:NilClass)

Can you update your MR to use your previous proposition ?

@dianedelallee
Copy link
Contributor Author

yep sure I will do that. Thanks for the explanation !

Co-authored-by: Alexandre Rousseau <contact@rousseau-alexandre.fr>
@madeindjs madeindjs merged commit 137a234 into madeindjs:master Jun 1, 2021
@madeindjs
Copy link
Owner

thank you for your contributions ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants