1

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

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

控制器 -> 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
4

2 回答 2

1

如果你让你的搜索方法返回一个空数组[]而不是nil一切都应该是好的。

它可能看起来像

   def self.search(params)
     tire.search(load: true) do
       query { string params[:query] } if params[:query].present?
     end || []
   end
于 2013-06-14T19:03:30.597 回答
0

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

于 2013-06-14T19:01:51.900 回答