我正在重定向到https,如下所示:
redirect_to :protocol => 'https://', :status => :moved_permanently
Run Code Online (Sandbox Code Playgroud)
但是,参数不会像这样经过.我可以像这样传递特定的参数:
redirect_to :protocol => 'https://', :status => :moved_permanently, :param1 => params[:param1], :param2 => params[:param2]
Run Code Online (Sandbox Code Playgroud)
我如何才能使它只是通过url上的每个参数而不必显式声明每个参数?
我目前有一个设置,在我的应用程序控制器中使用此before_filter强制SSL或http我需要它:
def force_ssl
if params[:controller] == "sessions"
if !request.ssl? && Rails.env.production?
redirect_to :protocol => 'https://', :status => :moved_permanently
end
else
if request.ssl? && Rails.env.production?
redirect_to :protocol => 'http://', :status => :moved_permanently
end
end
end
Run Code Online (Sandbox Code Playgroud)
我想做的是https://secure.example.com在使用SSL时使用,但http://example.com在不使用SSL时继续使用.有没有办法可以在主机名之间切换,具体取决于我是否使用SSL?
我正在尝试在我的请求规范中测试cookie:
require 'spec_helper'
describe "Cookies"
it "should set correctly" do
request.cookies['foo'] = 'bar'
end
end
Run Code Online (Sandbox Code Playgroud)
但这给了我undefined method 'cookies' for nil:NilClass.我该如何解决?
http://www.example.com每当我root_url在测试中使用时,我都会得到它.
它在开发中工作正常,我在config/environments/development.rb中有这个:
Rails.application.routes.default_url_options[:host]= 'localhost:3000'
Run Code Online (Sandbox Code Playgroud)
但是,添加它在config/environments/test.rb中不起作用.我应该添加什么才能localhost:3000在测试环境中用作主机?
我有一个Product和每个产品has_many ratings.我正在尝试创建一个:highest_rated产品范围,按照最高平均评级对产品进行订购(每个评级都在ratings表中).我试过这个:
scope :highest_rated, includes(:ratings).order('avg(ratings.rating) DESC')
Run Code Online (Sandbox Code Playgroud)
但这给了我一个misuse of aggregate: avg()错误.
有关如何以最高平均评级订购我的产品的任何提示?
我有一个非常基本的RSpec示例不起作用.这是代码:
require 'spec_helper'
describe "Referral-type functionality" do
describe "Affiliate system" do
before { @affiliate = Affiliate.create(account_id: 1) }
subject { @affiliate }
describe "URL should work" do
visit @affiliate.aff_url
end
end
end
Run Code Online (Sandbox Code Playgroud)
但是当我运行它时,rspec会给出NoMethodError,因为@affiliate是nil.我错过了什么?