Rails 5.2 Devise局部视图提交时PUT而非POST的问题求助
Let's break down the issues in your code and fix them step by step:
1. Resolve the Method Mismatch Between Form and Route
The core problem here is conflicting method settings:
- You defined a POST route for
update_swing_code, but yoursimple_form_forexplicitly sets:html => { :method => :put } - On top of that, your submit button adds an extra
method: :postwhich creates confusion for Rails.
Fix:
Update your partial's simple_form_for declaration to match your route's POST method, and remove the redundant method from the submit button:
<%= simple_form_for(@user, as: :user, url: update_swing_code_path(@user), method: :post) do |f| %> <!-- ... rest of your form ... --> <%= f.button :submit, "Update My Swing Code", class: "btn btn-yellow", controller: "registrations" %> <% end %>
Note: Changed as: User to as: :user (symbol instead of class) to avoid potential issues, and added @user to update_swing_code_path to generate the correct URL with the user's ID.
2. Fix Parameter Retrieval in Controller
Your hidden field uses :string_code as the name, but your controller tries to access params[:swing_code]—this means the value will never reach your swing_code attribute.
Fix:
Update the hidden field tag to use the correct parameter name:
<%= hidden_field_tag(:swing_code, id: "hiddenStringCode") %>
3. Correct Flash Message Placement
In your controller, you set flash[:notice] after redirect_to—Rails won't process flash messages after a redirect, so the notice will never show up.
Fix:
Move the flash message before the redirect, and add basic error handling for failed saves:
def update_swing_code @user = current_user # No need to call User.find(current_user.id) since current_user is already the instance if @user.update(swing_code: params[:swing_code]) flash[:notice] = "Your swing code has been saved!" redirect_to user_path(@user) else flash[:alert] = "Failed to save your swing code. Please check your input." redirect_to request.referer # Send user back to the form page end end
4. Verify Route Configuration
Your route uses :id as the parameter, but your error message shows /users/fullswing/update_swing_code—if fullswing is your user's username (not numeric ID), you need to adjust the route to match your User model's to_param setting.
If your User uses username for routing (e.g., you added def to_param; username; end to the User model):
Update the route to use :username instead of :id:
devise_scope :user do post "/users/:username/update_swing_code" => "registrations#update_swing_code", as: "update_swing_code" end
This ensures the generated URL matches the route definition.
5. Refine JavaScript Input Handling
Your JS builds the swing code correctly, but you can add a small improvement to handle empty fields more cleanly:
$(document).ready(function() { $(".code-digit").keyup(function () { if (this.value.length == this.maxLength) { $(this).next('.inputs').focus(); } // Collect input values and format the code let part1 = $("#field01, #field02, #field03").map((i, el) => el.value).get().join(''); let part2 = $("#field04, #field05, #field06").map((i, el) => el.value).get().join(''); let part3 = $("#field07, #field08, #field09, #field10").map((i, el) => el.value).get().join(''); let swing_code = `${part1}-${part2}-${part3}`; $("#swing_code").val(swing_code); // Updated to match the hidden field's ID }); });
After applying these fixes, your form should send a POST request to the correct route, pass the swing code parameter properly, and show the success/error messages as expected.
内容的提问来源于stack exchange,提问作者Liz

