Reputation: 1015
I am getting undefined method `each' for nil:NilClass in my index.html.erb, when I am implementing the elastic search functionality using tire in my model. What can be the possible reasons and solution for this?
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 %>
Model ( only the required code )-> 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
Upvotes: 1
Views: 1768
Reputation: 6426
If you make your search method return an empty array []
rather than nil
all should be good.
It could look something like
def self.search(params)
tire.search(load: true) do
query { string params[:query] } if params[:query].present?
end || []
end
Upvotes: 1
Reputation: 2614
This error is telling you that @user_friendships is nil when it is sent the each message. Presumably there is an issue occurring with how @user_friendships is being assigned in your params[:list] case statement. You'll need to figure out why @user_friendships is nil when the view is being rendered by troubleshooting this params[:list] case statement. I'd recommend debugging the application or logging the value of params[:list] / @user_friendships to see which case is being invoked and then figure out why the case being invoked is resulting in a nil value. Good luck!
Upvotes: 0