Passing a variable to the controller using link_to and use it on another page?

80 Views Asked by At

I have 2 pages and 2 partials. On the first page, I used jQuery to toggle between the 2 partials.

By default, the page displays partial_one.

This is on the first page with the toggle between 2 partials:

<div class="col-lg-offset-2 col-lg-4 btn-bar">
    <a class="btn-eg btn-egActive" data-rel="#partialOne" style="color: #fff;">Partial One</a>
    <a class="btn-eg" data-rel="#partialTwo" style="color: #fff;">Partial Two</a>
  </div>

<div class="showcase" id="partialOne" style="display: block;">
  <%= render 'partial_one' %>
</div>

<div class="showcase" id="partialTwo" style="display: none;">
  <%= render 'page/partial_two' %>
</div>

On the second page, is it possible to use a link_to button go to the first page and have the toggle defaulted as partial_two instead of partial_one?

I want to avoid making a duplicate page just for it, but not sure how to pass something to the controller that I can use on the first page to see if it was through the link_to button.

EDIT:

Thank you for the suggestion below, I was able to get this working.

I passed in the parameter as suggested:

link_to 'somewhere', my_path(ap: 1)

Then in my view page, I used an if statement to check the ap parameter:

<% if params[:ap] == "1" %> 

If the params ap is 1, then it came from link_to and the page would display partial_two. Otherwise the page displays partial_one by default.

1

There are 1 best solutions below

0
On BEST ANSWER

You could include a parameter in your GET request.

Example:

link_to 'somewhere', my_path(ap: 1)

which will generate

domain.com/some_path?ap=1

Then you can check the ap parameter easily.