I've a simple todo list app,and in the main page I've a button that brings up a new simple_form to add a new task. I'm using ajax so that the form appears in the same page (i.e. the main page).
My question is this, how to show validation error in the same form on the same page (i.e. the main page) without showing the form in another page (already done that).
My files are ,
items_controller.rb
def create
@item = Item.new(secure_params)
if @item.save
flash[:notice] = "Form submitted by #{@item.title}"
if flash[:isUpdate]
@var = Item.find(flash[:toDestroy]).destroy
end
redirect_to root_path
else
respond_to do |format|
format.html {render action: "_add"}
end
end
end
home.html.erb
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th><%= sortable "title"%></th>
<th><%= sortable "work_time"%></th>
<th>Creation Date</th>
<th>Expiration Date </th>
</tr>
</thead>
<tbody>
<% @items.each do |item|%>
<tr>
<td><%= link_to simple_format("#{item.title}"), items_show_single_record_path(:id =>item.id), remote: true%></td>
<td><%= item.work_time%></td>
<td><%= item.creation_date%></td>
<td><%= item.expiration_date%></td>
</tr>
<% end %>
</tbody>
</table>
</div>
<div id="items-modal"></div>
_add.html.erb
<div class ="modal-dialog modal-content modal-body">
<%= simple_form_for @item do |form|%>
<%= form.error_notification %>
<%= form.input :title%>
<%= form.input :creation_date, :readonly => true %>
<%= form.input :expiration_date%>
<%= form.input :work_time %>
<%= form.input :completed %>
<%= form.button :submit, 'Submit', class: 'submit' %>
<% end %>
</div>
add.js.erb
$("#items-modal").html("<%= j render 'add' %>")
$("#items-modal").modal("add")
I've looked at different answers here but no luck. Also, I'm new at this, so explanation and/or resources are appreciated.
An update:
I've added remote: true
to the form in _add.html.erb, now if there's an error in the form and I click submit, it displays the form in the same view, but it doesn't show the errors!