xxyyxx
xxyyxx

Reputation: 2396

How do I tell my rails form to use https?

I have a rails app and on this particular page (uses SSL) I have a form for created 'posts' and then separate form used for uploading images that belong to the post.

The image form is:

<%= form_for Image.new, :html => {:multipart => true} do |g| %>
<%= g.text_field :whence, :id => "postWhence"%>
<%= g.file_field :image, :id => "postImage_file_field", multiple: true, name: "image[image]" %>
<%= g.file_field :image, :id =>"postLogo_file_field"%>
<% end %>

I've seen people add :secure => true and :protocol => 'https', but that only seems to fit in if the format of the form for is something like form_for @user, :url => user_path(:secure => true).

The reason I'm trying to do this is because when I deployed to my production server, image uploading suddenly did not work, and when checking the log I saw:

Filter chain halted as :ensure_proper_protocol rendered or redirected
Completed 302 Found in 2ms (ActiveRecord: 0.0ms)" when I attempted to upload the image.

Upvotes: 1

Views: 719

Answers (1)

bennick
bennick

Reputation: 1917

You should just override the url as you alluded to. Something like:

<%= form_for Image.new, :url => image_path(:protocol => 'https'), :html => {:multipart => true} do |g| %>

Here are the docs on using url_for. http://api.rubyonrails.org/classes/ActionDispatch/Routing/UrlFor.html#method-i-url_for

The url option in form_for takes the same fields you pass to url_for or link_to.

Upvotes: 1

Related Questions