views:

65

answers:

1

I have an app that I've been developing that has so far only been a single user environment. I finally got to the point where I needed to add multi-user capabilities so I followed the railscast on authlogic to get a simple login.

I then added a has_many :items and accepts_nested_attributes_for :items and a belongs_to :user to the correct models. I then dropped the database and setup and then migrated it. I also added a user_id column to all my nested models.

After that, when I click on the "Create new item" link, I go to the new page and create a new item. When I go back to the item_index page, it's not showing up anymore. I can go to localhost/item/1 and see the record, so I know that it's being created, but when I try to view it in my item_index.html.erb it doesn't show up anymore.

Here's the basic loop that was working before I added the user. (It's rendering into a table)

<% for item in @items %>
    <%= link_to item.name, item %>
<% end %>

I imagine that the loop is what's wrong, but I'm not entirely sure.

Thanks

edit: Here's what's happening in my index method in my item controller:

   def index
     @items = Item.search params[:search]
     if @items.nil?
       @items = Item.all
     end
   end

I have the weird if nil? thing because I'm using thinking-sphinx and it was failing sometimes if the index was empty.

edit2:

If I change the index to have just

def index
   @items = Item.all
end

Everything shows up. So that means that it has to do with thinking sphinx messing with my render

edit3: in thinking-sphinx fashion, I did some things unrelated to it, and it magically works again.

A: 

Thinking Sphinx is going to be returning an empty result set [], when you try to iterate over this empty set you're not going to get any items shown.

To my knowledge, Thinking Sphinx will never return nil for a search result.

Perhaps try this instead:

if @items.empty?
  @items = Item.all
end
Ryan Bigg
I added the index method to my OP in the edit.
Reti
Also, I tried to do `for item in @items; item.name; end;` in the rails console and it said that I had a nil object when I didn't expect it. If I execute `Item.all` it shows my items.
Reti
@Reti: I've updated my answer to reflect your edits now. It should answer your question clearly.
Ryan Bigg
If Item.search returns an array with nil entries in it, your index is out of sync with your database. Try re-indexing and restarting the sphinx daemon.
James Healy