Ruby on rails Rails 5.2通过Put not Post部分提交设计编辑
我正在使用一个partial来编辑我的designeRuby on rails Rails 5.2通过Put not Post部分提交设计编辑,ruby-on-rails,devise,Ruby On Rails,Devise,我正在使用一个partial来编辑我的designeUser表的一列,当我点击submit时,它默认为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/reg
User
表的一列,当我点击submit时,它默认为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 %>
然而,当我点击提交按钮时(尽管有明确的指导,它使用method::post
),我仍然看到死亡的红白屏幕上写着:
没有与[PUT]匹配的路由“/users/fullswing/update\u swing\u code”
有人能帮我弄清楚如何让这个家伙提交吗?对于这样一个简单的任务,调试时间太长了!它使用
PUT
而不是POST
,因为在simple\u form\u For
调用中,您使用html=>{:method=>:PUT}
。如果要使用POST
,应将的简单表单更改为使用html=>{:method=>:POST}
并从您的f.button
调用中删除method::post
。不过,请记住,Rails的惯例是使用PATCH
进行更新,而不是post
。不过,您不需要添加HTTP方法。Rails将从实例变量推断该方法。如果是新记录,Rails将使用ePOST
HTTP方法,如果不是新记录,Rails将使用PATCH
HTTP方法
了解
<% @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>
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