2010-12-04 86 views
14

我想有一個鏈接使用SSL。我使用此代碼:link_to與:協議不起作用

<%= link_to "Buy now!", line_items_path(:thing_id => @thing), :method => :post, :protocol => "https", :only_path => false %> 

出於某種原因,正與http://代替https://生成的鏈接。

我正在使用Rails 3.0.3。

回答

24

你必須把:協議選項的路徑幫手裏面:

<%= link_to "Buy now!", line_items_url(:thing_id => @thing, :protocol => "https"), :method => :post %> 
+3

如果我沒有弄錯,:only_path => false是必須的。只有在指定了host的情況下才能省略它。 – plang 2011-01-07 15:38:50

1

如果你在你的應用程序中使用bartt-ssl_requirement ruby​​gem,您可以使用ssl-url-helper明確提到HTTP或HTTPS協議。

優點:

  1. 如果您已經停用SSL檢查(開發環境),通過以下方式:SslRequirement.disable_ssl_check = true,然後通過:secure => true不會HTTPS鏈接明確地添加到您的視圖。如果您指定:protocol => 'https'並禁用SSL檢查,則情況並非如此。

  2. 另外,不需要在每個地方將line_items_path更改爲line_items_url