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?
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With