can anyone explain how to show error_messages_on (like back in Rails
2 , without Ajax) fields that didnt’ pass the validation the jquery
way.
I googled for about 2 hours now and found nothing. Jquery works fine
and adds the content to my table, but im totally stuck with the whole
error/validation thing.
My form looks like this:
<%= form_for Translation.new , :remote => true do |f| %>
Create action like this:
def create
if @translation.save
flash[:notice] = “Successfully created translation.”
respond_to do |format|
format.html { redirect_to @translation }
format.js
end
else
respond_to do |format|
format.html { render :action => ‘new’}
# show what went wrong with jquery
end
end
end
and create.js.erb
/* Insert a notice between the last comment and the comment form */
$("#translation-notice").html(’
<%=
escape_javascript(flash.delete(:notice)) %>
’);
/* Add the new comment to the bottom of the comments list */
$("#translations").prepend("<%=
escape_javascript(render(@translation)) %>");
/* Highlight the new comment */
$("#translation-<%= @translation.id %>").effect(“highlight”, {},
3000);
/* Reset the comment form */
$("#new_translation")[0].reset();
Adding validated data works but please give me hint with error
validation thing.
def create
if @translation.save
flash[:notice] = “Successfully created translation.”
respond_to do |format|
format.html { redirect_to @translation }
format.js
end
else
respond_to do |format|
format.html { render :action => ‘new’}
# show what went wrong with jquery
end
end
you can pass an action to format.js, apparently since most examples dont
pass anything you didnt notice that format,js is doing this
format.js { render :action => “create”} and calls a create.js.erb
it does this if you only put format.js but you can render other actions
like
this
def create
if @translation.save
flash[:notice] = “Successfully created translation.”
respond_to do |format|
format.html { redirect_to @translation }
format.js { render :action => “success”}
end
else
respond_to do |format|
format.html { render :action => ‘new’}
format.js { render :action => “failure”}
end
end
and have an success.js.erb with code for displaying the “everything went
cool” updates and a failure.js.erb to display the “epic fail” updates.
there is no need to add a success or failure actions to the controller,
when
rails does not find and action in a controller it tries to display a
template with the corresponding name anyway.