Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add parameters to a link_to external URL?

In my Show User page, I want to add a link to an external website with some values saved in the table passed as parameters.

I got the first part working right, that's simple.

<%= link_to "Outside Site", "http://outsidesite.com/create" %>

But I also want to pass some paramaters which are saved in the database, like @user.email, @user.first_name, etc.

So basically the final link will look like:

http://outsidesite.com/[email protected]&firstname=userfirstname etc etc.

What would be the best way to do this?

like image 673
cdotfeli Avatar asked Dec 23 '11 02:12

cdotfeli


2 Answers

You can write the helper method such as url patern. You can check the code bellow:

  def generate_url(url, params = {})
    uri = URI(url)
    uri.query = params.to_query
    uri.to_s
  end

After that, you can call the helper method like:

generate_url("YOUR-URL-ADDR-HERE", :param1 => "first", :param2 => "second")

I hope you find this useful.

like image 177
miksiii Avatar answered Nov 12 '22 20:11

miksiii


This is a valid approach:

<% = link_to "Outside Site", 
"http://outsidesite.com/create?email=#{@user.email}" %>

Just make sure you escape the variables you're putting into the URL:

require 'uri'
escaped_email = URI.escape(@user.email)
like image 4
kasrak Avatar answered Nov 12 '22 21:11

kasrak