Kevin
Kevin

Reputation: 15954

Create a link in a partial that keeps the parent view's URL parameters

I have a view with a partial in it, like this:

# my_controller/my_view
The value of the parameter is <%= @parameter %>
<%= render :partial "my_contoller/my_partial", locals: {internal_param: @parameter_for_partial %>

#my_controller/my_partial
<div>
  The value of the internal parameter is <%= internal_param %><br />
  <%= link_to "Link", {:partial_url_param => internal_param + 1} %>
</div>

The controller action takes URL parameters:

def class MyControllerController < ApplicationController
  def my_action
    @parameter = params[:parent_view_url_param]
    @parameter_for_partial = params[:partial_url_param]
  end
end

Now, when I go to /my_controller/my_view?parent_view_url_param=1&partial_url_param=2, I get a page that looks like this:

The value of the parameter is 1
<div>
  The value of the internal parameter is 2<br />
  <a href="/my_controller/my_view?partial_url_param=3">Link</a>
</div>

I want the link to point to /my_controller/my_view?parent_view_url_param=1&partial_url_param=3, with all of the URL parameters from the original link the same except the ones that the partial directly overrides. Moreover, if I added more parameters, I would want the link in the partial to keep those as well, without requiring me to make any changes to the partial.

Is there a way to do this in Rails 4?

Upvotes: 0

Views: 120

Answers (2)

Kevin
Kevin

Reputation: 15954

Using the method in bgates' answer, I created this helper method and put it in application_helper.rb:

  # Creates a link back to the current view, keeping all of
  # the URL parameters the same except some that you
  # change. Also strips out some URL parameters that can
  # have implications for the app's security.
  #
  # Input params:
  # link_name: The text to show for the link
  # original_params: The params hash available to the view
  # params_to_change: A hash in the format of
  #   orignal_params that indicates which URL parameters
  #   to change, and what to change them to
  # html_options (optional): The HTML options to apply
  #   to the link
  #
  # Output: The HTML for a link back to the view with some
  #   of the URL parameters changed, as specified by
  #   the params_to_change hash
  def link_to_with_same_params(link_name, original_params, params_to_change, html_options = {})
    send_params = original_params.deep_dup
    send_params.delete :authenticity_token
    send_params.delete :commit
    send_params.merge! params_to_change
    return link_to link_name, send_params, html_options
  end

Which allows me to do this in my example view:

#my_controller/my_partial
<div>
  The value of the internal parameter is <%= internal_param %><br />
  <%= link_to_with_same_params "Link", params, {:partial_url_param => internal_param + 1} %>
</div>

Upvotes: 0

bgates
bgates

Reputation: 2373

How about

=link_to 'Link', params.merge(partial_url_param: internal_param + 1)

Upvotes: 1

Related Questions