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

13,669

1) stringify_keys is a method that is called on a hash to convert its keys from symbols to strings. It's added by Rails - it's 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 is passing "/users/sign_in" somewhere that is expecting a hash. Closer inspection reveals that you are mixing and matching two forms of link_to:

# 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 block form to be the options hash, so it is calling stringify_keys on it which is causing your error.

Change those links to look like this instead:

<%= link_to destroy_user_session_path, :method => 'delete' do %>
  <i class=" icon-user icon-black"></i> Sign out 
<% end %>
Share:
13,669
Siddharth
Author by

Siddharth

Updated on June 14, 2022

Comments

  • Siddharth
    Siddharth almost 2 years

    In a partial file of my application I have the following code snippet for displaying user navigation(through 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'm getting an error saying:-

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

    Now my questions are:-

    1. What is `stringify_keys' in general??
    2. How do I resolve this in my code???

    Thanks...

  • Siddharth
    Siddharth over 11 years
    Let me mention you something that this error happens when I'm adding "do" and "end" condition to my "link_to" helper. That is if my link_to block simply looks like:- <li> <%= link_to "Sign out", destroy_user_session_path, :method => 'delete' %> <i class=" icon-user icon-black"></i> </li>
  • sameera207
    sameera207 over 11 years
    can u write the link_to helper with your 'do' 'end' tags
  • thenextmogul
    thenextmogul over 9 years
    Wow, it works for me also! Saved me from more hours of headache and pain because of this issue. Tx!