ruby-on-rails - 在我的index.html.erb中为nil:NilClass获取未定义的方法 `each'

标签 ruby-on-rails ruby elasticsearch tire

当我在模型中使用Tire实现 flex 搜索功能时,我在index.html.erb中得到了nil:NilClass的未定义方法“each”。可能的原因和解决方案是什么?

index.html.erb:

    <div class="page-header">
  <h1>Friends</h1>

<%= form_tag user_friendships_path, method: :get do %>
      <p>
            <%= text_field_tag :query, params[:query] %>
            <%= submit_tag "Search", name: nil %>
      </p>
<% end %>


</div>

<div>
      <strong>Friend list:</strong>
      <%= link_to 'All', user_friendships_path %> |
      <%= link_to 'Blocked', user_friendships_path(list: 'Blocked') %> |
      <%= link_to 'Requested', user_friendships_path(list: 'Requested') %> |
      <%= link_to 'Accepted', user_friendships_path(list: 'Accepted') %> |
      <%= link_to 'Pending', user_friendships_path(list: 'Pending') %>
</div>
<% @user_friendships.each do |friendship| %>
<% friend = friendship.friend %>
<div id="<%= dom_id(friendship) %>" class="friend row">
      <div class="span1">
            <%= link_to image_tag(friend.gravatar_url), profile_path(friend) %>
      </div>
      <div class="span7">
            <strong><%= friend.full_name %></strong><br />
            <% if friendship.pending? %>
            <em>Friendship is pending.</em> <%= link_to "Delete request", edit_user_friendship_path(id: friend.user_name) %>.
            <% end %>
            <% if friendship.requested? %>
            <em>Friendship requested.</em> <%= link_to "Accept Friendship", edit_user_friendship_path(id: friend.user_name) %>.
            <% end %>
            <% if friendship.accepted? %>
            <em>Friendship started <%= friendship.updated_at %>.</em> <%= link_to "Update friendship", edit_user_friendship_path(id: friend.user_name) %>.
            <% end %>
            <% if current_user.blocked_friends.include?(friend) %>
            <%= form_for friendship, url: user_friendship_path(friendship), method: :delete do |form| %>
                  <%= submit_tag "Unblock", class: 'btn btn-danger' %>
            <% end %>  
            <%end%>
      </div>
</div>
<% end %>

模型(仅所需的代码)-> user_friendship.rb:
    class UserFriendship < ActiveRecord::Base

include Tire::Model::Search
include Tire::Model::Callbacks


  def self.search(params)
     tire.search(load: true) do
       query { string params[:query] } if params[:query].present?
     end
   end

  attr_accessible :user, :friend, :user_id, :friend_id, :state

  belongs_to :user

Controller -> user_friendship_controller.rb
 class UserFriendshipsController < ApplicationController


    before_filter :authenticate_user!
  respond_to :html, :json

    def index


    @user_friendships = current_user.user_friendships.all.search(params)


    case params[:list]
    when nil
      @user_friendships = current_user.pending_user_friendships.all + current_user.accepted_user_friendships.all + current_user.requested_user_friendships.all
    when 'Blocked'
      @user_friendships = current_user.blocked_user_friendships.all
    when 'Pending'
      @user_friendships = current_user.pending_user_friendships.all
    when 'Accepted'
      @user_friendships = current_user.accepted_user_friendships.all
    when 'Requested'
      @user_friendships = current_user.requested_user_friendships.all
    end
    respond_with @user_friendships
  end

最佳答案

该错误告诉您@user_friendships在发送每条消息时为nil。大概在params [:list]案例语句中如何分配@user_friendships会出现问题。您需要通过对此params [:list]案例语句进行故障排除来弄清楚为什么在呈现 View 时@user_friendships为零。我建议调试应用程序或记录params [:list] / @user_friendships的值,以查看正在调用哪种情况,然后弄清楚为什么调用该情况会导致nil值。祝好运!

关于ruby-on-rails - 在我的index.html.erb中为nil:NilClass获取未定义的方法 `each',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17115037/

相关文章:

ruby-on-rails - 当记录似乎是唯一的时,为什么我得到 'ActiveRecord::RecordNotUnique:'?

ruby-on-rails - Ruby:我怎样才能制作一个显示的组合框

ruby - 在 Ruby 中,计算数字 e 时出现意外结果

jdbc - 使用 logstash 将数据从 MySQL 索引到 ElasticSearch 时,它显示 Java heapspace 错误

maven - 使用Eclipse通过JAVA API访问Elasticsearch

elasticsearch - KIbana没有显示新创建的索引

jQuery 只加载一次

ruby-on-rails - 用于跟踪验证错误的 Rails 插件

ruby-on-rails - 如何在新建和创建操作之间传递参数?

ruby-on-rails - Rails sitemap_generator 未初始化常量?