首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails 5.2通过部分提交通过Put not Post设计编辑

Rails 5.2通过部分提交通过Put not Post设计编辑
EN

Stack Overflow用户
提问于 2020-04-19 13:36:17
回答 1查看 79关注 0票数 0

我使用一个分部来编辑我设计的User表的一列,当我点击submit时,它将默认为put而不是post

视图如下所示:

代码语言:javascript
复制
<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 %>

有了这个部分:

代码语言:javascript
复制
<% @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 = params:swing_code @user.save redirect_to user_path(@user)闪存:注意:“您的swing代码已经保存了!”结束

我的routes有:

代码语言:javascript
复制
  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的明确指导,我仍然看到红色和白色的死亡屏幕上写着:

没有路由匹配输入“/user/fullswing/update_swing_code”

有人能帮我弄清楚怎么让这家伙上交吗?对于这样一个简单的任务,它需要太长时间来调试!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-19 14:58:58

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

阅读关于资源路由的文章

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61305235

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档