0

Inside of a rails app that I am working, I modified the link_to helper slightly:

  def link_to(*args, &block)
    args[1] = params[:client_id].present? ? "#{args[1]}?client_id=#{params[:client_id]}" : args[1]
    super
  end

I did this so I wouldn't have to add the :client_id => params[:client_id] every time I wrote a link_to inside of the app. Well, I have kind of pigeon holed myself with the following problem...

If I have this link_to:

<%= link_to "Continue to billing info", add_product_path(:product_id => @product.id), :class => 'btn' %>

Using my link_to helper creates a link, like so:

http://localhost:3001/orders/add_product?product_id=35?client_id=HT274848772

I am at a slight loss on how to modify my helper so that the link will work as normal while including the :client_id param...

dennismonsewicz
  • 25,132
  • 33
  • 116
  • 189
  • Have you seen the source code of link_to? https://github.com/rails/rails/blob/380800e4a272db98f3dd267f82e0ce1e4d03f4b8/actionpack/lib/action_view/helpers/url_helper.rb#L231 – Ray Shih Jan 04 '13 at 17:15
  • Yes, I have... I was just trying to come up with a solution so I wasn't adding in the client_id param every time I wrote a link_to.. any suggestions? – dennismonsewicz Jan 04 '13 at 17:23

1 Answers1

1

You want to add your parameter to the link url, not to the link itself. Maybe you should rewrite the url_for helper, which is the helper used by all of the url helpers ( http://apidock.com/rails/ActionView/Helpers/UrlHelper/url_for )

ChuckE
  • 5,610
  • 4
  • 31
  • 59
  • I've never really used url_for before, can you provide an example? – dennismonsewicz Jan 04 '13 at 17:32
  • url_for(:controller => "babies_controller", :action => "kill", :id => 69) is usually the result of a kill_baby_url(69) call. you want to add your client_id there all the time, I suppose. – ChuckE Jan 04 '13 at 21:16