# How to have \*\_url generate urls with https:// protocol

**URL:** <https://www.ruby-forum.com/t/how-to-have-url-generate-urls-with-https-protocol/189254>\
**Category:** Rails\
**Created:** [May 27, 2010, 3:41pm UTC](https://www.ruby-forum.com/t/how-to-have-url-generate-urls-with-https-protocol/189254 "2010-05-27T15:41:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![errolsiegel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c77e96/32.png) [@errolsiegel](https://www.ruby-forum.com/u/errolsiegel)\
**Post date:** [May 27, 2010, 3:41pm UTC](https://www.ruby-forum.com/t/how-to-have-url-generate-urls-with-https-protocol/189254/1 "2010-05-27T15:41:07Z")

</div>

Hello,

I accidentally posted this to the “Ruby” forum when it should have gone  
here – sorry.

I am using \*\_path in views and \*\_url in controllers (with redirect\_to,  
for example). It is my understanding that this is generally considered  
a good practice.

I want to use my app using https, and thought it would work that way as  
long as I loaded the app by browsing to [https://my.app.com](https://my.app.com). However, it  
appears that even if I do this, redirect\_to some\_model\_url generates a  
url with a protocol of https://.

How do I get \*\_url to generate a url with https:// for the protocol?

Regards,  
Errol

---

<div class="post-metadata">

**Author:** ![errolsiegel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c77e96/32.png) [@errolsiegel](https://www.ruby-forum.com/u/errolsiegel)\
**Post date:** [May 27, 2010, 6:47pm UTC](https://www.ruby-forum.com/t/how-to-have-url-generate-urls-with-https-protocol/189254/2 "2010-05-27T18:47:41Z")

</div>

> long as I loaded the app by browsing to [https://my.app.com](https://my.app.com). However, it  
> appears that even if I do this, redirect\_to some\_model\_url generates a  
> url with a protocol of https://.

I assume you meant to say “http://” ?

> How do I get \*\_url to generate a url with https:// for the protocol?

some\_model\_url(:scheme =\> ‘https’)

-philip

---

<div class="post-metadata">

**Author:** ![errolsiegel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c77e96/32.png) [@errolsiegel](https://www.ruby-forum.com/u/errolsiegel)\
**Post date:** [May 27, 2010, 7:11pm UTC](https://www.ruby-forum.com/t/how-to-have-url-generate-urls-with-https-protocol/189254/3 "2010-05-27T19:11:52Z")

</div>

> I assume you meant to say “http://” ?

Yes! I thought I proof-read so well…

> > How do I get \*\_url to generate a url with https:// for the protocol?
> 
> some\_model\_url(:scheme =\> ‘https’)

Thanks for the response. I guess I was looking for a way to do this  
application-wide. It would not be very DRY to have to add (:scheme =\>  
‘https’) to every place in my app where I call a \*\_url helper.

Is there another way?

---

<div class="post-metadata">

**Author:** ![errolsiegel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c77e96/32.png) [@errolsiegel](https://www.ruby-forum.com/u/errolsiegel)\
**Post date:** [May 27, 2010, 8:35pm UTC](https://www.ruby-forum.com/t/how-to-have-url-generate-urls-with-https-protocol/189254/4 "2010-05-27T20:35:21Z")

</div>

> Use \*\_path? I do it and haven’t seen any browsers complain about the  
> redirects…  
> Again, that would mean going through the entire application and changing  
> every reference.

Besides, using relative paths in redirects violates the HTTP spec. I  
understand that it gets the job done, but that doesn’t seem like the  
right way to do it.

I don’t wish to appear argumentative, but I really hope somebody knows a  
cleaner way to do this.

---

<div class="post-metadata">

**Author:** ![errolsiegel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c77e96/32.png) [@errolsiegel](https://www.ruby-forum.com/u/errolsiegel)\
**Post date:** [May 27, 2010, 7:35pm UTC](https://www.ruby-forum.com/t/how-to-have-url-generate-urls-with-https-protocol/189254/5 "2010-05-27T19:35:07Z")

</div>

On May 27, 2010, at 10:11 AM, Errol S. wrote:

> ‘https’) to every place in my app where I call a \*\_url helper.
> 
> Is there another way?

Use \*\_path? I do it and haven’t seen any browsers complain about the  
redirects…

---

<div class="post-metadata">

**Author:** ![errolsiegel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c77e96/32.png) [@errolsiegel](https://www.ruby-forum.com/u/errolsiegel)\
**Post date:** [May 27, 2010, 11:36pm UTC](https://www.ruby-forum.com/t/how-to-have-url-generate-urls-with-https-protocol/189254/6 "2010-05-27T23:36:08Z")

</div>

Errol S. wrote:

> > Use \*\_path? I do it and haven’t seen any browsers complain about the  
> > redirects…  
> > Again, that would mean going through the entire application and changing  
> > every reference.
> 
> Besides, using relative paths in redirects violates the HTTP spec. I  
> understand that it gets the job done, but that doesn’t seem like the  
> right way to do it.
> 
> I don’t wish to appear argumentative, but I really hope somebody knows a  
> cleaner way to do this.

Use default\_url\_options.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![errolsiegel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c77e96/32.png) [@errolsiegel](https://www.ruby-forum.com/u/errolsiegel)\
**Post date:** [May 28, 2010, 11:36am UTC](https://www.ruby-forum.com/t/how-to-have-url-generate-urls-with-https-protocol/189254/7 "2010-05-28T11:36:54Z")

</div>

Hi Errol,

If you want to pull out data in csv format from an existing mysql  
database using Rails, this is the easiest way to do it :

1. install fastercsv : sudo gem install fastercsv
2. Add on top of your controller : require ‘fastercsv’
3. Create an action in your controller and its corresponding view

# The action

def import\_csv  
result = YourModel.find(:all, :conditions =\> “your\_condition”)  
@outfile = “your\_csv\_report\_name\_” + Time.now.strftime("%m-%d-%Y") +  
“.csv”  
csv\_data = FasterCSV.generate do |csv|  
#field names  
csv \<\< [“Field\_name\_1”]  
csv \<\< [“Field\_name\_2”]  
#Data  
result.each do |table|  
csv \<\< [table.field1 table.field2]  
end  
#Write data in csv format  
send\_data csv\_data,  
:type =\> ‘text/csv; charset=iso-8859-1; header=present’,  
:disposition =\> “attachment; filename=#{@outfile}”

```
redirect_to :action => "your_action"

```

end  
end

#The view  
\<% form\_tag :action =\> “import\_csv” do %\>  
\<%= submit\_tag ‘Import CSV’ %\>  
\<% end %\>

Hope this helps!

Regards,
