【问题标题】:Rails 5.2 Devise Edit via Partial Submitting via Put not PostRails 5.2 通过 Put not Post 部分提交设计编辑
【发布时间】:2020-08-01 22:31:46
【问题描述】:

我正在使用部分编辑我的 Devise User 表的一列,当我点击提交时,它默认为 put 而不是 post

视图如下所示:

<h4 class="color-white font-weight-bold responsive-heading">
 Add your code to your profile now!
</h4>
<% if current_user %>
  <%= render partial: "devise/registrations/update_swing_code" %>
<% else %>
  <%= link_to "Create My Free Account", new_user_registration_path, class: "btn btn-yellow" %>
<% end %>

有了这个部分:

<% @user = current_user %>

<%= simple_form_for(@user, as: User, :url => update_swing_code_path, :html => { :method => :put }) do |f| %>
  <%= hidden_field_tag(:string_code, id: "hiddenStringCode") %>

  <section class="form-inputs">
    <div class="form-group text-center">
      <input id="field01" type="text" class="form-control display-3 font-typewriter inline-block code-digit inputs" maxlength="1" style="width: 50px; height: 70px; font-size: 2rem" >
      <input id="field02" type="text" class="form-control display-3 font-typewriter inline-block code-digit inputs" maxlength="1" style="width: 50px; height: 70px; font-size: 2rem" >
      <input id="field03" type="text" class="form-control display-3 font-typewriter inline-block code-digit inputs" maxlength="1" style="width: 50px; height: 70px; font-size: 2rem" >
      -
      <input id="field04" type="text" class="form-control display-3 font-typewriter inline-block code-digit inputs" maxlength="1" style="width: 50px; height: 70px; font-size: 2rem" >
      <input id="field05" type="text" class="form-control display-3 font-typewriter inline-block code-digit inputs" maxlength="1" style="width: 50px; height: 70px; font-size: 2rem" >
      <input id="field06" type="text" class="form-control display-3 font-typewriter inline-block code-digit inputs" maxlength="1" style="width: 50px; height: 70px; font-size: 2rem" >
      -
      <input id="field07" type="text" class="form-control display-3 font-typewriter inline-block code-digit inputs" maxlength="1" style="width: 50px; height: 70px; font-size: 2rem" >
      <input id="field08" type="text" class="form-control display-3 font-typewriter inline-block code-digit inputs" maxlength="1" style="width: 50px; height: 70px; font-size: 2rem" >
      <input id="field09" type="text" class="form-control display-3 font-typewriter inline-block code-digit inputs" maxlength="1" style="width: 50px; height: 70px; font-size: 2rem" >
      <input id="field10" type="text" class="form-control display-3 font-typewriter inline-block code-digit inputs" maxlength="1" style="width: 50px; height: 70px; font-size: 2rem" >
    </div>
 </section>

 <div class="text-center">
   <%= f.error_notification %>
 </div>

  <div id="submitButton" class="form-actions mt-3">
    <%= f.button :submit, "Update My Swing Code", class: "btn btn-yellow", controller: "registrations", method: :post %>
  </div>

<% end %>



<script>
  $(document).ready(function() {
    $(".code-digit").keyup(function () {
        if (this.value.length == this.maxLength) {
          $(this).next('.inputs').focus();
        }
        var swing_code = "";
        swing_code = $("#field01").val() + $("#field02").val() + $("#field03").val() + "-" + $("#field04").val() + $("#field05").val() + $("#field06").val() + "-" + $("#field07").val() + $("#field08").val() + $("#field09").val() + $("#field10").val()
        $("#string_code").val( swing_code );
    });
  });
</script>

我的registrations_controller有这个方法:

def update_swing_code @user = User.find(current_user.id) @user.swing_code = 参数[:swing_code] @user.save 重定向到用户路径(@user) flash[:notice] = "您的挥杆代码已保存!" 结束

我的routes 有这个:

  devise_for :users, :controllers => { registrations: 'registrations' }
  resources :users, only: [:show, :index]
  get 'users/show'
  get 'users/index'
  devise_scope :user do
   post "/users/:id/update_swing_code" => "registrations#update_swing_code", as: "update_swing_code"
  end

然而,当我点击提交按钮时(尽管它使用method: :post 的明确指导,我仍然得到红白相间的死亡屏幕说:

没有路线匹配 [PUT] "/users/fullswing/update_swing_code"

谁能帮我弄清楚如何让这个人提交?对于这样一个简单的任务,调试时间太长了!

【问题讨论】:

    标签: ruby-on-rails devise


    【解决方案1】:

    它使用PUT 而不是POST,因为在您的simple_form_for 通话中,您使用的是html =&gt; { :method =&gt; :put }。如果您想使用POST,您应该将您的simple_form_for 更改为使用html =&gt; { :method =&gt; :post },并从您的f.button 呼叫中删除method: :post。不过请记住,Rails 约定是使用PATCH 进行更新,而不是POST。不过,您不需要添加 HTTP 方法。 Rails 将根据实例变量推断方法。如果是新记录,Rails 将使用POST HTTP 方法,如果不是新记录,Rails 将使用PATCH HTTP 方法。

    了解Resource Routing

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-22
      • 2018-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多