在我的应用程序的部分文件中,我有以下代码片段用于显示用户导航(通过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>
但我得到一个错误说: –
undefined method `stringify_keys' for "/users/sign_in":String
现在我的问题是: –
>一般来说`stringify_keys’是什么?
>我如何在我的代码中解决这个问题?
谢谢…
解决方法
1)stringify_keys是一个在哈希上调用的方法,用于将其键从符号转换为字符串.它是由Rails添加的 – 它不是标准的Ruby方法.
Here it is in the docs.
{:a => 1,:b => 2}.stringify_keys # => {"a" => 1,"b" => 2}
2)这意味着你的代码在某个期望散列的地方传递“/ users / sign_in”.仔细检查显示您正在混合和匹配两种形式的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
正如您所看到的,您正在尝试两者:
<%= link_to "Sign out",:method => 'delete' do %> <i class=" icon-user icon-black"></i> <% end %>
并且Rails期望块形式中的第二个参数是选项哈希,因此它会调用stringify_keys,这会导致您的错误.
<%= link_to destroy_user_session_path,:method => 'delete' do %> <i class=" icon-user icon-black"></i> Sign out <% end %>