Мне нужна помощь здесь,
Я получаю сообщение об ошибке при попытке обновить модель с помощью ajax из Rails (form_with / remote: true). Я смог нормально работать с XHR-запросами на URL, которые были ресурсами Rails (см. Маршруты ниже), но с пользовательскими 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
не имеет определенного формата (может быть, в этом проблема?).
test.log:
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: -> (req) { req.xhr? }
- Как я уже говорил, я делал то же самое (тесты, контроллер) для других ресурсов, используя XHR из
form_with
, и они работают нормально. Этот с пользовательским URL, который не работал.
- Рельсы 5.2 и Rspec 3.6
- Любой вопрос, просто спросите в комментариях
Заранее спасибо!