I have a customer model that has_many events When I go to the customer show page, I have links to all of the events that are owned by customer. I want to add a “New event for this customer” link. Right now, I’m doing that with <%= link_to "New Event for this Customer", new_event_path %> but when I follow that link, I have to manually enter in the customer’s id number. How can I automate this so that when I click “Add new event for this customer” rails knows that I’m adding a new event for that particular customer instead of having to put in the customer_id myself?
I have a customer model that has_many events When I go to the customer
Share
What you are looking for is called
nested resources, here’s a good introduction.For this specific case you should declare your routes like these:
The above declaration would allow you to define routes like:
And in your specific case: