Monday, February 21, 2011

Creating a link, partially from stored data, with rails

Hi, I'm a rails newbie.

I'm trying to create a link on a show page, with part of the data coming from a stored field which is the subdomain. I can't save the full link before the save in the model because I'm using just the sub-domain info for a script that's running.

So, for example, I'm saving "subdomain" in the database but on a show page want to display, and link to:

http://<%=h @user.subdomain %>.thissite.com

I've tried a few ways to do this and can't get it working. I'd really appreciate any assistance....

From stackoverflow
  • From the documentation:

    link_to "Visit Other Site", "http://www.rubyonrails.org/"
    

    so, translating that to your need that becomes:

    link_to "Click here", "http://#{@user.subdomain}.thissite.com/"
    
    Scott S. : This will work, but it can get messy if you want to use the restful route helpers in rails along with that code.
    Andrew : Thank you for the help! And for the documentation link....I appreciate it.
  • I think you're looking for:

    link_to 'test', :host => "#{@user.subdomain}.yoursite.com", :path_only => false

    You can find more information here: http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html Be sure to look at the options in url_for.

    If you want all links you create to have the subdomain, you can also do this in your application controller:

    def default_url_options(options)
       { :only_path => false, :host => "#{@user.subdomain}.yoursite.com" }
    end
    

0 comments:

Post a Comment