Ruby on rails 当我在Rails中发出POST请求时,会话ID会更改,但仅在测试期间更改

Ruby on rails 当我在Rails中发出POST请求时,会话ID会更改,但仅在测试期间更改,ruby-on-rails,ruby,ruby-on-rails-4,csrf,rspec-rails,Ruby On Rails,Ruby,Ruby On Rails 4,Csrf,Rspec Rails,我有一个相当奇怪的问题,那就是通过method::POST链接到,触发了POST请求。在测试环境中,会话id似乎发生了变化。这会导致诸如当前用户对象在我发布的操作中不存在之类的问题。我已经注销了请求和会话信息,我可以看到会话已经为POST操作更改,当我尝试使用当前用户时,测试失败 我通过应用程序周围的表单有其他POST请求。它们工作得很好。通过method::post将link\u链接到,并传递CSRF令牌,Rails似乎有点神奇 我可以通过将我的test.rb更改为与我的development

我有一个相当奇怪的问题,那就是通过
method::POST
链接到
,触发了POST请求。在测试环境中,
会话id
似乎发生了变化。这会导致诸如当前用户对象在我发布的操作中不存在之类的问题。我已经注销了请求和会话信息,我可以看到会话已经为POST操作更改,当我尝试使用当前用户时,测试失败

我通过应用程序周围的表单有其他POST请求。它们工作得很好。通过
method::post
link\u链接到
,并传递CSRF令牌,Rails似乎有点神奇

我可以通过将我的
test.rb
更改为与我的
development.rb
相同来解决这个问题。但我相信这不是一个好的解决方案。它可能与某些配置有关,但似乎这是默认行为

控制器

class RecruitersController < ApplicationController
  before_action -> { STDOUT.puts "Request: #{request.method} #{request.fullpath}" }
  before_action -> { STDOUT.puts "Session: #{session[:session_id]}" }

  ...
end    
发展中的产出

= link_to "<3", recruiter_request_url(id: recruiter.id), method: :post, remote: true
Request: GET /recruiters/dashboard
Session: cdb333efb5d62e6ddbb5914c8edd7a92
Request: GET /recruiters/premium
Session: cdb333efb5d62e6ddbb5914c8edd7a92
Request: POST /recruiters/request_premium_trial/1
Session: cdb333efb5d62e6ddbb5914c8edd7a92
规范 Simple spec用户登录进入仪表板,进入高级页面,然后单击发出POST请求的链接

scenario 'Should be able to make request', js:true do
  rsign_in # Function that simulates sign in
  click_on 'Premium'
  click_on '<3'
  assert_text 'Request made' # Fails as we're redirected to sign in page when we try to authenticate the user
end
Development.rb

Rails.application.configure do
  # Set log level
  config.log_level = :debug

  # This means that all URLs need to have 5 parts to them. This is for http://recruiter.127.0.0.1.xip.io:3000
  config.action_dispatch.tld_length = 5
  # Settings specified here will take precedence over those in config/application.rb.

  cache_store = :file_store, "tmp/cache"
  # Use a different job queue
  config.active_job.queue_adapter = Figaro.env.job_queue.to_sym if Figaro.env.job_queue?

  # The test environment is used exclusively to run your application's
  # test suite. You never need to work with it otherwise. Remember that
  # your test database is "scratch space" for the test suite and is wiped
  # and recreated between test runs. Don't rely on the data there!
  config.cache_classes = true

  # Do not eager load code on boot. This avoids loading your whole application
  # just for the purpose of running a single test. If you are using a tool that
  # preloads Rails for running tests, you may have to set it to true.
  config.eager_load = true

  # Configure static asset server for tests with Cache-Control for performance.
  config.serve_static_files  = true
  config.static_cache_control = 'public, max-age=3600'

  # Show full error reports and disable caching.
  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = false

  # Raise exceptions instead of rendering exception templates.
  config.action_dispatch.show_exceptions = false

  # Disable request forgery protection in test environment.
  config.action_controller.allow_forgery_protection = false

  # Tell Action Mailer not to deliver emails to the real world.
  # The :test delivery method accumulates sent emails in the
  # ActionMailer::Base.deliveries array.
  config.action_controller.default_url_options = { host: 'localhost:5000' }
  config.action_mailer.default_url_options = { host: 'localhost:5000' }
  config.action_mailer.delivery_method = :test
  # config.action_mailer.delivery_method = :smtp
  # config.action_mailer.smtp_settings = { address: 'localhost', port: '1025' }
  # Print deprecation notices to the stderr.
  config.active_support.deprecation = :stderr

  config.active_record.raise_in_transactional_callbacks = true

  # Raises error for missing translations
  # config.action_view.raise_on_missing_translations = true
end
Rails.application.configure do
  # Settings specified here will take precedence over those in config/application.rb.

  # In the development environment your application's code is reloaded on
  # every request. This slows down response time but is perfect for development
  # since you don't have to restart the web server when you make code changes.
  config.cache_classes = false

  # Do not eager load code on boot.
  config.eager_load = false

  # Show full error reports and disable caching.
  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = false

  # Don't care if the mailer can't send.
  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.preview_path = "#{Rails.root}/app/mailers/previews"

  # Print deprecation notices to the Rails logger.
  config.active_support.deprecation = :log

  # Raise an error on page load if there are pending migrations.
  config.active_record.migration_error = :page_load
  config.action_mailer.default_url_options = { host: 'localhost:3000' }
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = { address: 'localhost', port: '1025' }

  config.cache_store = :dalli_store
  # Use a different job queue
  config.active_job.queue_adapter = Figaro.env.job_queue.to_sym if Figaro.env.job_queue?

  # Debug mode disables concatenation and preprocessing of assets.
  # This option may cause significant delays in view rendering with a large
  # number of complex assets.
  config.assets.debug = true

  # Adds additional error checking when serving assets at runtime.
  # Checks for improperly declared sprockets dependencies.
  # Raises helpful error messages.
  config.assets.raise_runtime_errors = true

  # Raises error for missing translations
  # config.action_view.raise_on_missing_translations = true

  config.after_initialize do
    Bullet.enable = false
    Bullet.alert = true
    Bullet.console = true
    Bullet.rails_logger = true
  end
end

似乎错误来自我的
test.rb
中的这一行,只是试图将我链接上的默认主机设置为not be example.com。不确定这是如何导致我遇到的错误的。但是在查看配置并试图找出
development.rb
工作的原因之后。这就是我得到的

config.action_controller.default_url_options = { host: 'localhost:5000' }

感谢所有帮助过我的人。

你能把相关的
规范
@Marwen包括进来吗?我已经添加了它。这很简单。只需转到页面并单击链接。我在我的
测试中添加了rb
,以防出现配置问题。您的规范中有一个输入错误。将
rsign\u in
更改为
sign\u in
@NeilAtkinson
rsign\u in
是我正在使用的一个函数,它模拟招聘人员登录的过程。我的主要问题是点击链接后的POST请求。由于某种原因,会话被重置,从而导致身份验证失败。在此处添加一条注释以表示相同。当您单击
config.action_controller.default_url_options = { host: 'localhost:5000' }