Rails form_for助手身份验证参数?

时间:2019-06-09 11:38:05

标签: ruby-on-rails remote-forms

我有一些遗留的rails代码,它们指定了authenticated:对于远程表单,它为true。但是据我所知,该参数没有任何作用。也许开发人员想要包括一个真实性令牌?

我进行了搜索,但没有找到有关此参数的文档。

<%= form_for @house, url: action_path, method: "PATCH", authenticated: true, remote: true, html: { id: 'house-edit-contact' } do |f| %>
    <%= f.label :website, 'website: ' %>
    <%= f.text_field :website %>
    <br>
    <%= f.label :phone, 'phone: ' %>
    <%= f.text_field :phone %>
    <br><br>
    <%= f.submit "Submit Changes" %>
  <% end %>

对于这里值得的是生成的HTML,但是在其中看不到与身份验证相关的任何内容:

<form class="edit_house" id="house-edit-contact" action="/houses/7112?field=contact-info&amp;placement=contact_info" accept-charset="UTF-8" data-remote="true" method="post">
  <input name="utf8" type="hidden" value="✓">
  <input type="hidden" name="_method" value="patch">
  <br>
  <label for="house_website">website: </label>
  <input type="text" value="portofhopecenters.org" name="house[website]" id="house_website">
  <br>
  <label for="house_phone">phone: </label>
  <input type="text" value="208-463-0118" name="house[phone]" id="house_phone">
  <br><br>
  <input type="submit" name="commit" value="Submit Changes" data-disable-with="Submit Changes">
</form>

1 个答案:

答案 0 :(得分:0)

你可以

<%= form_for @house, url:(action_path, authenticated: true), method: "PATCH", remote: true, html: { id: 'house-edit-contact' } do |f| %>
    <%= f.label :website, 'website: ' %>
    <%= f.text_field :website %>
    <br>
    <%= f.label :phone, 'phone: ' %>
    <%= f.text_field :phone %>
    <br><br>
    <%= f.submit "Submit Changes" %>
  <% end %>

但是在使用devise时,您无需从view传递它 您只需在before_action: authenticate_user!, only: [:update]本身的controller中进行控制。