ruby-on-railssimple-form-for

Simple Form make remote throwing errors


I'm having trouble getting my simple_form not working when I try to make it remote. I don't want to refresh the whole page and just the area that has the form. Currently, I have this:

<div class="col-2">
    <%= simple_form_for ([current_user, @character]), defaults: {label: false} do |m| %>
        <%= m.simple_fields_for :dnd5e_sheet do |p| %>
            <%= p.input :acrobatics, as: :boolean, input_html: { onchange: 'this.form.submit();' } %>
        <% end %>
    <% end %>
</div>

Which works but the whole page refreshes, so I added a new route:

resources :user, only: [:show] do
    resources :characters do
      put "minus-hp", to: "characters#minus_hp"
      put "add-hp", to: "characters#add_hp"
      patch "skill-update", to: "characters#skill_update"
    end
    resources :campaigns
  end 

And in the controller I made a new action:

def skill_update
    @character = Character.includes(:dnd5e_sheet)
                          .find(params[:character_id])
    @character.update(character_params)
    respond_to do |format|
      format.js { render partial: 'characters/DnD5e/skills' }
    end
  end

The skill.js.erb just has a simple render:

$("#stats").html("<%= escape_javascript(render partial: 'characters/DnD5e/skills_card') %>");

I've tried every combination of URL paths I could think of and couldn't get any of them to work.

Edit: Ive tried this as well:

<%= simple_form_for @character, url: user_character_skill_update_path(current_user, @character), method: :put, remote: true, :authenticity_token => true, defaults: {label: false} do |m| %>
                          <%= m.simple_fields_for :dnd5e_sheet do |p| %>
                            <%= p.input :acrobatics, as: :boolean, input_html: { onchange: 'this.form.submit();' } %>
                          <% end %>
                        <% end %>

but it sends it as an HTML request instead of javascript so get an error in the respond_to


Solution

  • So found out from this question Rails Trying to submit a form onchange of dropdown

    What was wrong was not the form or remote true, but the onchange event, so changed it to this:

    <%= m.input :acrobatics, input_html: { onchange: 'Rails.fire(this.form, "submit")' } %>
    

    and everything works as expected for a remote form