By adding the :remote => true
attribute, you can make the button trigger an asynchronous (Ajax) call.
index.html.erb
<%= link_to "Add Journal", new_journal_path, remote: true, class: 'new_journal_button'%>
new.js.erb
$('.new_journal_button').after("<%= j render('form') %>");
$('.new_journal_button').hide();
If you wish to submit a form asynchronously using Ajax
_form.html.erb
<%= form_for(@journal, remote: true) do |f| %>
<div class="field">
<%= f.label "journal" %><br>
<%= f.text_field :title, placeholder: 'Journal', autofocus: true, 'data-behavior' => 'submit_on_enter' %>
</div>
<% end %>
Journals_controller.rb
def index
@journal = Journal.new
@journals = Journal.all
end
def create
@journal = Journal.new(journal_params)
respond_to do |format|
if @journal.save
format.html { redirect_to @journal, notice: 'Journal was successfully created.' }
format.js
else
format.html { render :new }
end
end
end