简体   繁体   中英

What's the best way to make a Ruby on Rails helper for your secure url path?

I come from a PHP and Python/GAE background and am new to RoR. Typically I'd have a global variable set up containing my unsecure and secure webpage's url, for use when templating. For example, if I want to link to an image, rather than saying img src="www.mysite.com/images/img1.png" I can use img src="{{ unsecure_url }}images/img1.png".

I'm thinking the best way to do this in RoR is to add the following to my application_helper. Is that valid?

def unsecure_url
  "http://localhost:3000/"
end

def secure_url
  "https://localhost:3000/"
end

And how can I switch between localhost and production, depending on whether I'm in a local environment or not?

If you use the pre-existing helpers, they should generate relative links for the current protocol and domain.

Take a look at link_to , and image_tag .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM