Skip to content Skip to sidebar Skip to footer

Rails/JS Button Onclick To Re-render Partial Without Entering Into DB

I wish to reload a partial-form with a button(Add). I'm new and don't know how to simply display fields in partial like listings one under the other as many times Add button is cli

Solution 1:

You don't necessary need to save things to pass away to .js.erb... you can just instantiate...

But if your example is precise, you are missing the remote: true flag for the link... And the partial is not defined on the link... you need to make a view responding to the ajax...

Form example

<div class="panel-body">
  <%= link_to new_thing_path, remote: true do %>
    <i class="fa fa-plus">
    Add another qualification
  <% end %>
  <div class="new-things-container">
  </div>
</div>

Controller answering to the ajax request

class ThingsController < ApplicationController
  def new
    @thing = Thing.new

    respond_with @thing
  end
end

View for the ajax request rendering a partial inside a specified div

//views/things/new.js.erb
$('.panel-body .new-things-controller').append("<%= render partial: 'degrees/form', locals: { thing: @thing } %>")

Post a Comment for "Rails/JS Button Onclick To Re-render Partial Without Entering Into DB"