1

I'm creating an app for creating multiple to-do lists. So a user signs in, has multiple lists, and each list contains multiple items. Everything else is working, but I'm struggling to create the items.

My code for creating a new item on a list (this is found in the items_controller) is :

def create

    @list =
    @new_item = @list.items.build(params[:item])
    if @new_item.save
      flash[:success] = "Item saved!"
    end
    redirect_to root_path
  end

And the issue is, I'm not sure how to define what @list should be. I have a variable current_user (based on the session) for creating a new list, but there is only one user per session and multiple lists per session, so I can't just replicate that method.

Basically, I'm stuck on how to be able to have the item know which list it belongs to (which should be the list whose show page I was just on). In Java I'd have a static variable that I would redefine every time I went to a list, but I tried doing that and it didn't work, and I read that apparently in rails that doesn't work.

4
  • Save your users lists into the data base - even in java web apps using a static variable is not a good idea Commented Jan 7, 2012 at 22:55
  • The lists are in the database--the trouble is that even though each list has its own id, the item doesn't know which specific list it belongs to Commented Jan 7, 2012 at 23:01
  • How can that be? Doesn't a list has_many items :through something, or habtm? Commented Jan 7, 2012 at 23:10
  • It does, but the item hasn't been created yet, so it doesn't belong to anything... Commented Jan 7, 2012 at 23:31

2 Answers 2

1

Rather than storing a variable, you should keep track of the list using url parameters. You will need to modify your form to include a list_id parameter. Then in the controller when you are creating a list item, do something like:

@list = List.find params[:list_id]
@new_item = @list.items.build params[:item]

Or if you made your params include an items[list_id] parameter, then in Rails it will be accessible in params[:item][:list_id] so you should just be able to just do:

@new_item = ListItem.create params[:item]

If you do it this second way, just be sure to add a validation in the ListItem model to guarantee that list_id is present and the list it points to exists.

Sign up to request clarification or add additional context in comments.

1 Comment

That's a good idea--I should keep track of it with url parameters. But really, I think, when I link to a List show page from the main home page, and then while on the list page the list_id will be accessible from params.
1

The listItem should have a foreign key to the list model, and the list model should have a foreign key to the account. In this way you should be able to traverse the structure easily.

class ListItem  < ActiveRecord::Base
  belongs_to :list
end

class List <  < ActiveRecord::Base
  belongs_to :account
  has_many :list_items
end

class Account <  < ActiveRecord::Base
  has_many :lists
end

1 Comment

That's exactly how my database is set up. However, in order to create the list item, I need to specify which list it should belong to--and I'm not sure how to specify that (@list) using the foreign key :list_id, since it doesn't belong to a list yet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.