Cameron Bass Cameron Bass - 1 month ago 5
Ruby Question

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

Right now I have a

link_to
. 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



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


HTML



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

Answer

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])
end

Then on your view page:

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