0
votes

I'm using a partial to edit just one column of my Devise User table and when I hit submit it's defaulting to put instead of post.

The view looks like this:

<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 %>

With this partial:

<% @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>

My registrations_controller has this method:

def update_swing_code @user = User.find(current_user.id) @user.swing_code = params[:swing_code] @user.save redirect_to user_path(@user) flash[:notice] = "Your swing code has been saved!" end

And my routes have this:

  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

Yet when I hit the submit button (despite the explicit guidance that it use method: :post, I still get the red and white screen of death saying:

No route matches [PUT] "/users/fullswing/update_swing_code"

Can anyone help me figure out how to get this guy to submit? For such a simple task it's taking waaaaay too long to debug!

1

1 Answers

1
votes

It's using PUT instead of POST because in your simple_form_for call, your using html => { :method => :put }. If you want to use POST, you should change your simple_form_for to use html => { :method => :post } and remove the method: :post from your f.button call. Do keep in mind, though, that Rails convention is to use PATCH to make updates, not POST. You shouldn't need to add a HTTP method, though. Rails will infer the method from the instance variable. If it is a new record, Rails will use the POST HTTP method and if it's not a new record, Rails will use the PATCH HTTP method.

Read about Resource Routing