@Stivie

Почему simple_form убирает токен при remote: true?

Имеется следующая форма:
= simple_form_for Message.new do |f|
    = f.input :body, as: :text
    = f.submit

Генерируется следующий HTML:
<form novalidate="novalidate" class="simple_form new_message" id="new_message" action="/messages" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="✓">
  <input type="hidden" name="authenticity_token" value="2d0pBcQTcmVOjV5UG8VwNPg74t65dYd0lnWNLoQC2NIqirzuf0ZzTga6oTwLKtbVmPzf1qOoKyGMEZxUdnkyuA==">
  <div class="input text required message_body">
    <label class="text required" for="message_body">
      <abbr title="required">*</abbr> Body</label>
    <textarea class="text required" name="message[body]" id="message_body"></textarea>
  </div>
  <input type="submit" name="commit" value="Create Message">
</form>

Но если добавить remote: true, то форма становится такого вида:
<form novalidate="novalidate" class="simple_form new_message" id="new_message" action="/messages" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="✓">
  <div class="input text required message_body">
    <label class="text required" for="message_body">
      <abbr title="required">*</abbr> Body</label>
    <textarea class="text required" name="message[body]" id="message_body"></textarea>
  </div>
  <input type="submit" name="commit" value="Create Message">
</form>

И появляется ошибка ActionController::InvalidAuthenticityToken
  • Вопрос задан
  • 444 просмотра
Пригласить эксперта
Ответы на вопрос 1
@eoffsock
Кодер (Rails)
Вообще simple_form наследует стандартное поведение form_for, а именно:
Remote forms may omit the embedded authenticity token by setting
config.action_view.embed_authenticity_token_in_remote_forms = false

api.rubyonrails.org/classes/ActionView/Helpers/For...

Так что попробуйте выставить этот параметр в true в application.rb.
Либо в simple_form_for писать :authenticity_token => true
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы