Cameron Bass Cameron Bass - 11 months ago 53
Ruby Question

Rails link_to query params - selecting in item on the next page

Right now I have a

. It looks like a button the the user. It's an upgrade button so it routes the user to the plans page.

I not only want to route the user to the plans page but I want to pre-select the plan that supports the feature they are upgrading for. I was told that I could pass a query parameter into the link_to? Is the true and what does this look like in my situation? Here is my code.


<%= link_to edit_account_plan_path, class: "button-big reverse-blue" do %>


<div class="plan-control">
<%= radio_button_tag :plan_id,, row.current?, disabled: row.ineligible? %>

Answer Source

you can pass a query parameter back to your controller something like this:

<%= link_to edit_account_plan_path(feature: 'basic'), class: "button-big reverse-blue" do %>

Then in your controller action routed by edit_account_plan_path, you can set an instance variable which you can use to preselect on your view page:

def edit
  @plan = Plan.find_by(feature: params[:feature])

Then on your view page:

<div class="plan-control">
    <%= radio_button_tag :plan_id, 'Basic', @plan.feature == 'basic' %>