【问题标题】:Rails 5.1: Post request not working correctlyRails 5.1:发布请求无法正常工作
【发布时间】:2017-06-25 08:36:43
【问题描述】:

在我的 Rails 5.1 应用程序中,我使用 devise-authy gem 将 2fa 添加到我的应用程序。用这种形式...

= enable_authy_form multipart: true do
    = text_field_tag :country_code, '', required: true, id: 'country_code', aria_required: 'true'
    = telephone_field :cellphone, '', required: true, autocomplete: 'off', id: 'authy-cellphone', class: 'string required', aria_required: 'true'
    = button 'button', I18n.t( 'enable_authy', { scope: 'devise' } ), nil, 'submit', true, 'flex-end'

...我发布到以下控制器操作 (from the gem):

  def POST_enable_authy
    @authy_user = Authy::API.register_user(
      :email => resource.email,
      :cellphone => params[:cellphone],
      :country_code => params[:country_code]
    )

    if @authy_user.ok?
      resource.authy_id = @authy_user.id
      if resource.save
        set_flash_message(:notice, :enabled)
      else
        set_flash_message(:error, :not_enabled)
        redirect_to after_authy_enabled_path_for(resource) and return
      end

      redirect_to [resource_name, :verify_authy_installation]
    else
      set_flash_message(:error, :not_enabled)
      render :enable_authy
    end
  end

但正如您从日志中看到的那样,没有任何反应:

Started POST "/2fa/enable" for 127.0.0.1 at 2017-06-24 21:19:02 +0200
Processing by Devise::DeviseAuthyController#POST_enable_authy as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"5P1aSzQvWATGEBLEYYwoD7dS/6sCSzpAk24rnh1DK9ZV6S70WWt10ijmoAo9MlxyGDAol+ewZYAszooiDeZKQQ==", "country"=>"United States", "country_code"=>"us", "cellphone"=>["546876578"], "subdomain"=>""}
Cookie#domain returns dot-less domain name now. Use Cookie#dot_domain if you need "." at the beginning.
  Rendering devise/devise_authy/enable_authy.html.haml within layouts/application
Completed 200 OK in 3632ms (Views: 2461.8ms | ActiveRecord: 1.5ms)

它只是重定向到请求来源的模板。我不明白为什么会这样。


即使在 fork gem 之后,修改控制器以重定向到特定的 URL,也没有任何反应。

【问题讨论】:

  • 我不确定发生了什么,下面 R_G 的建议可能会有所帮助。我只是想知道为什么在这种情况下您的表单设置为multipart: true?
  • @philnash Dunno,我删除了它:)
  • 哦,我想我可能知道这里发生了什么。您在哪里设置 Authy API 密钥?你能确认你确实在设置它吗?
  • @philnash 在初始化程序 (auth.rb) 中:Authy.api_key = Settings.devise.authy.key - 除非 Authy.api_key 有问题,否则我相信这会按预期工作。
  • 我会检查它是否从秘密中正确设置。尝试在您的控制器中记录Authy.api_key,看看会发生什么。

标签: ruby-on-rails post twilio ruby-on-rails-5 authy


【解决方案1】:

我不确定它是否适用,因为我在使用 Ruby/Sinatra 时遇到过这种情况。我的帖子似乎是正确的,但它一直返回原始表格。问题出在默认的 AJAX 上。由于 AJAX 打算更新表单,否则它不会重定向。我指定 data-ajax="false" 来解决它。让我知道它是否有帮助。 (发表评论作为答案。)

【讨论】:

  • @johnue 有趣。你能告诉我你做了什么吗?是否有什么不同?
  • 好吧,到目前为止我还没有找到解决方案。我尝试了您的建议,显然尝试通过使用分叉存储库来更改控制器操作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-14
  • 1970-01-01
  • 1970-01-01
  • 2014-11-03
  • 2014-04-21
  • 2021-04-17
  • 2018-07-13
相关资源
最近更新 更多