What is `stringify_keys' in rails and how to solve it when this error occurs?

In the partial file of my application, I have the following code fragment for displaying user navigation (via Devise): -

<ul class="nav pull-right"> <% if user_signed_in? %> <li> <%= current_user.email do %> <i class=" icon-user icon-black"></i> <% end %> </li> <li> <%= link_to "Your Links", profiles_index_path do %> <i class=" icon-user icon-black"></i> <% end %> </li> <li> <%= link_to "Sign out", destroy_user_session_path, :method => 'delete' do %> <i class=" icon-user icon-black"></i> <% end %> </li> <% else %> <li> <%= link_to "Login", new_user_session_path do %> <i class=" icon-lock"></i> <% end %> </li> <li> <%= link_to "Sign up", new_user_registration_path do %> <i class=" icon-home"></i> <% end %> </li> <% end %> </ul> 

But I get an error: -

 undefined method `stringify_keys' for "/users/sign_in":String 

Now my questions are: -

  • What is stringify_keys in general ??
  • How do I resolve this in my code ???

Thanks...

+6
source share
2 answers

1) stringify_keys is a method that is called for a hash to convert its keys from characters to strings. It is added by Rails - this is not a standard Ruby method. Here it is in the docs .

 {:a => 1, :b => 2}.stringify_keys # => {"a" => 1, "b" => 2} 

2) This means that your code skips "/users/sign_in" somewhere waiting for a hash. A closer look reveals that you are mixing and matching two link_to forms:

 # specify link contents as an argument link_to "The text in the link", "/path/to/link", some: "options" # specify link contents in a block link_to "/path/to/link", some: "options" do "The text in the link" end 

As you can see, you are trying to do both:

 <%= link_to "Sign out", destroy_user_session_path, :method => 'delete' do %> <i class=" icon-user icon-black"></i> <% end %> 

and Rails expects the second argument in the form of a block to be a hash of parameters, so it calls stringify_keys on it, which causes your error.

Modify these links to look like this:

 <%= link_to destroy_user_session_path, :method => 'delete' do %> <i class=" icon-user icon-black"></i> Sign out <% end %> 
+23
source

according to the document stringify_keys will try to convert all the keys to a string. Thus, in simple works, the method expects something with key values ​​and converts its keys into strings.

In your case, most likely your User object may be empty or a plan line. it's not a job to try to publish a complete error log

0
source

All Articles