Search code examples
ruby-on-railsactiverecordrouteslink-to

Rails generates correct URL but always loads first Record


I have an application with a Bill ActiveRecord model. I want to include on a page a link "Back to bill", which will send the user to the "show" page of that bill. Right now I have

<% link_to "Back to bill", bill_url(bill_id) %>

Where bill_id is the correct id of the bill I want to show.

When I click on the link, I am redirected to the correct url (as displayed in the browser). However, regardless of what the bill_id is, the bill which is shown is only the first bill record in the "bills" table in my database.

Why is the redirect happening incorrectly, and what method should I use to correctly render the bill that matches the generated URL?


Solution

  • Make sure your show action in BillsController has the following:

    @bill = Bill.find(params[:id])