使用Rails 3的未定义方法`stringify_keys'错误

时间:2015-04-13 06:38:46

标签: ruby ruby-on-rails-3 will-paginate

当我根据下拉值设置搜索数据的表单时,我收到以下错误。

错误:

 NoMethodError in Users#index

Showing C:/Site/searchtest/app/views/users/index.html.erb where line #4 raised:

undefined method `stringify_keys' for nil:NilClass
Extracted source (around line #4):

1: <h1>Listing users</h1>
2: <%= form_tag users_path, :method => 'get' do  %>  
3:   <p>  
4:     <%= text_field_tag(:search,options_for_select([['subhrajyotipradhan@gmail.com','subhrajyotipradhan@gmail.com'],['subhrajyoti.pradhan@oditeksolutions.com','subhrajyoti.pradhan@oditeksolutions.com'],['w5call.w5rtc@gmail.com','w5call.w5rtc@gmail.com']],selected: "Select your email"), params[:search]) %>
5:     <%= submit_tag "Search", :name => nil %>  
6:   </p>  
7: <% end %> 

我的目标是当用户从下拉列表中选择选项时,应相应地过滤该值。请检查我的下面的代码,并尝试帮助我使其正确。

的观点/用户/ index.html.erb

<h1>Listing users</h1>
<%= form_tag users_path, :method => 'get' do  %>  
  <p>  
    <%= text_field_tag(:search,options_for_select([['subhrajyotipradhan@gmail.com','subhrajyotipradhan@gmail.com'],['subhrajyoti.pradhan@oditeksolutions.com','subhrajyoti.pradhan@oditeksolutions.com'],['w5call.w5rtc@gmail.com','w5call.w5rtc@gmail.com']],selected: "Select your email"), params[:search]) %>
    <%= submit_tag "Search", :name => nil %>  
  </p>  
<% end %> 
<table>
  <tr>
    <th>Name</th>
    <th>Email</th>
    <th>Content</th>
    <th></th>
    <th></th>
    <th></th>
  </tr>

<% @users.each do |user| %>
  <tr>
    <td><%= user.name %></td>
    <td><%= user.email %></td>
    <td><%= user.content %></td>
    <td><%= link_to 'Show', user %></td>
    <td><%= link_to 'Edit', edit_user_path(user) %></td>
    <td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td>
  </tr>
<% end %>
</table>

<br />

<%= link_to 'New User', new_user_path %>

我想使用will_paginate gem来实现此功能。请帮助我。

2 个答案:

答案 0 :(得分:0)

您无法使用selected: "Select your email"。它必须是您的选项之一,因此selected: 'subhrajyotipradhan@gmail.com'selected: subhrajyoti.pradhan@oditeksolutions.comselected: 'w5call.w5rtc@gmail.com'

答案 1 :(得分:0)

你可以试试这个。我希望这会对你有所帮助

select_tag "search", options_for_select(['subhrajyotipradhan@gmail.com', 'subhrajyotipradhan@gmail.com'], 'subhrajyotipradhan@gmail.com')
相关问题