【问题标题】:Rails form_with (remote: true) errorsRails form_with (remote: true) 错误
【发布时间】:2018-08-29 22:11:03
【问题描述】:

我需要一些帮助,

当我尝试使用 Rails 中的 ajax 更新模型时出现错误(form_with / remote: true)。我能够很好地处理 Rails 对作为资源的 url 的 XHR 请求(请参阅下面的路线),但是使用自定义 url 我遇到了错误。

控制器:

def criar
  @user = current_user
  respond_to do |format|
    if @user.update_attributes(user_params)
      format.js {
        flash[:success] = "Success!"
        redirect_to root_path
      }
    else
      format.js 
    end
  end
end

rspec(请求):

put user_criar_path(user), xhr: true,
 :params => { ... }

查看:

<%= form_with model: @user, url: user_criar_path(@user), method: :put do |f| %>

路线:

namespace :any do
  namespace :things do
    put '/criar', to: 'user#criar'         # problem with XHR
    put '/atualizar', to: 'user#atualizar' # problem with XHR
  end
end

resources :anything   # this one works fine with XHR

正如您在 test.log 中看到的,Processing by UserController#criar as 没有特定的格式(也许这就是问题所在?)。

测试日志:

Processing by UserController#criar as 
  Parameters: { ... }

错误信息:

Failure/Error:
  respond_to do |format|
    if @user.update_attributes(user_params)
      format.js {
        flash[:success] = "Success!"
        redirect_to root_path
      }
    else
      format.js 
    end

ActionController::UnknownFormat:
ActionController::UnknownFormat

另一个请求测试

it "should be redirect to (criar)" do
  put user_criar_path(1), xhr: true
  expect(response).to redirect_to(new_session_path)
  expect(request.flash_hash.alert).to eq "To continue, please, sign in."
end

错误信息

Failure/Error: expect(response).to redirect_to(new_session_path)
  Expected response to be a <3XX: redirect>, but was a <401: Unauthorized>
  Response body: To continue, please, sign in.

观察:

  • 我已经尝试将路由上的 url 更改为: put '/criar', to: 'user#criar', constraints: -&gt; (req) { req.xhr? }
  • 正如我之前所说,我正在使用来自form_with 的 XHR 对其他资源执行相同的操作(测试、控制器),并且它们运行良好。这个自定义网址不起作用。
  • Rails 5.2 和 Rspec 3.6
  • 有任何问题,请在 cmets 上提问

提前致谢!

【问题讨论】:

    标签: rspec rspec-rails ruby-on-rails-5.2


    【解决方案1】:

    按照建议尝试在 respond_to 块之前调用 request.xhr? here

    【讨论】:

    • 感谢您的帮助,我找到并发布了解决方案!
    【解决方案2】:

    问题原因

    好吧,经过一番搜索,我找到了答案。

    ActionController::UnknownFormat 消息相关的问题是由于请求没有很好地定义,正如我们在我发布的日志中看到的那样:

    由 UserController#criar 处理

    句尾缺少类型/格式(HTML/JS/...等)。

    问题是由两个因素引起的:

    1. 在 Rspec 上使用 rails 生成的路径并将参数传递给它:

    user_criar_path(1)put user_criar_path(user), xhr: true, params: {...}

    1. 定义我自己的路线 (routes.rb)

    get '/user/new', to: 'user#new' # defining my own route

    而不是

    resources: user, only: [:new]#Rails 定义

    观察:对于 Rails(资源)定义的路由,将参数传递给生成路径不会引发错误 ActionController::UnknownFormat

    解决方案

    从生成路径中删除参数(对于 Rspec 和 Rails):

    put user_criar_path, params: { "user" => { "id" => 1 } }
    

    put user_criar_path, xhr: true, :params => { ... }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-06
      • 2012-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-12
      • 2018-11-05
      相关资源
      最近更新 更多