Reputation: 309
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?
Upvotes: 5
Views: 5500
Reputation: 2496
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.
Upvotes: 6
Reputation: 7480
Give this a try
# routes.rb
get 'create?:query' => 'users#create', :as => :create_users_with_query
# models/user.rb
class User < ActiveRecord::Base
def to_params
"email=#{self.email}&firstname=#{self.firstname}"
end
end
# view
<%= link_to "Outside Site", create_users_with_query(@user) %>
Upvotes: -2
Reputation: 470
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)
Upvotes: 4
Reputation: 11689
Because rails don't know how the website want it's paramteres, I think you must do it with string concatenation. At least, you can write a helper to do this for you but will just become a string concatenation in the end.
Upvotes: 1