When adding :remote => true to my edit form in my rails app, submits work fine, but now my program does not show error messages. I have tried multiple solutions but none of them have worked. Can anyone point me in the correct direction to solve this problem? If the entry is successfully saved, a notice message is added to a div, something I wrote in edit.js.erb. I tried to also have the ‘error messages’ added to an error message div but @entry.errors.any? never returns true when I add :remote => true.
This is the edit div in my form I am trying to update.
<div id="errors">
<% if @entry.errors.any? %>
<h2><%= pluralize(@entry.errors.count, "error") %> prohibited this entry from being saved:</h2>
<ul>
<% @entry.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>
</div>
I also have its contents in a partial and my edit.js.erb tries to update the div with the newly rendered partial, but now @entry.errors.any? is not returning true here anymore.. Here is also my controller…
# GET /entries/1/edit
def edit
@entry = Entry.find(params[:id])
respond_to do |format|
format.html
format.js
end
end
def update
@entry = Entry.find(params[:id])
respond_to do |format|
if @entry.update_attributes(params[:entry])
format.html { redirect_to edit_entry_path(@entry), notice: 'Entry was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @entry.errors, status: :unprocessable_entity }
end
end
end
I created a partial, _errors.html.erb
I created update.js.erb
where #errors refers to a in my form, and the errors partial. Finally my controller is like this now.
# PUT /entries/1
# PUT /entries/1.json
def update
@entry = Entry.find(params[:id])
Also, since I only want ajax on my edit form, and not my create form (adding :remote => true to _form.html.erb effects both) I also changed this line in _form.html.erb from
to
Thank you for the help, those who helped me achieve my solution.