Rails 5.2 通过 Put not Post 通过部分提交设计编辑
Rails 5.2 Devise Edit via Partial Submitting via Put not Post
我正在使用部分编辑我的 Devise User
table 的一列,当我点击提交时它默认为 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
redirect_to user_path(@user)
闪光[:通知] = "Your swing code has been saved!"
结束
我的 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
的明确指导,我仍然看到死亡的红白屏幕说:
No route matches [PUT] "/users/fullswing/update_swing_code"
谁能帮我弄清楚如何让这个人提交?对于如此简单的任务,调试时间太长了!
它使用 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 方法。
我正在使用部分编辑我的 Devise User
table 的一列,当我点击提交时它默认为 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 redirect_to user_path(@user) 闪光[:通知] = "Your swing code has been saved!" 结束
我的 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
的明确指导,我仍然看到死亡的红白屏幕说:
No route matches [PUT] "/users/fullswing/update_swing_code"
谁能帮我弄清楚如何让这个人提交?对于如此简单的任务,调试时间太长了!
它使用 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 方法。