I am building a Kid’s Chore App. In application.html.erb I show a side bar listing the children’s names:
<div id="side">
<%= link_to "Home", home_path %><br />
<% @children.each do |child| %>
<%= link_to child.name, child_path(child.id) %><br />
<% end %>
</div>
Upon clicking a child’s name, I want all chores to be displayed.. The code above will “show” the child when clicked.
I am thinking something like:
<%= link_to child.name, chore_path %><br />
.. this does not work because:
- I then lose the child_id… which I need for them to record their chores
- I am routed to http://localhost:3000/chores/1 (I just want the chores index)
How can I keep the child_id as a variable in this example but display the chore index?
Cheers, Chris
Associations below:
class Child < ActiveRecord::Base
has_many :completion, :dependent => :destroy
has_many :chores, :through => :completion
class Chore < ActiveRecord::Base
has_many :completions
has_many :kids, :through => :completions
class Completion < ActiveRecord::Base
belongs_to :child
belongs_to :chore
Then you can grab the child id in the action with
params[:child_id]The link will be to
/chores?child_id=1for example.