2010-12-04 88 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