Rails 3 + jQuery ; How to show error messages

193 views
Skip to first unread message

mrpink

unread,
Sep 27, 2010, 8:44:02 AM9/27/10
to Ruby on Rails: Talk
Hi guys,

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| %>
<table>
<tr>
<td>
<%= f.label :locale %><br />
<%= f.text_field :locale %>

</td>
<td>
<%= f.label :key %><br />
<%= f.text_field :key %>
</td>
</tr>
<tr>
<td>
<%= f.label :value %><br />
<%= f.text_area :value , :rows => 3%>
</td>
<td>
<%= f.label :interpolations %><br />
<%= f.text_area :interpolations, :rows => 3 %>
</td>
</tr>
<tr>
<td>
<%= f.label :is_proc %><br />
<%= f.check_box :is_proc %>
</td>
<td></td>
</tr>
</table>
<p><%= f.submit t('translation.create') %></p>
<% end %>

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('<div class="flash notice"><%=
escape_javascript(flash.delete(:notice)) %></div>');

/* 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.

thanks

radhames brito

unread,
Sep 27, 2010, 11:15:04 AM9/27/10
to rubyonra...@googlegroups.com

the problem is

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.


Sönke Buhr

unread,
Sep 27, 2010, 11:19:10 AM9/27/10
to rubyonra...@googlegroups.com
Thanks mate.

2010/9/27 radhames brito <rbr...@gmail.com>


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

radhames brito

unread,
Sep 27, 2010, 11:16:51 AM9/27/10
to rubyonra...@googlegroups.com
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.

Sönke Buhr

unread,
Sep 27, 2010, 11:33:47 AM9/27/10
to rubyonra...@googlegroups.com
so, rename create.js.erb to success.js.erb and create a failure.js.erb and refer to format.js in the controller?

2010/9/27 radhames brito <rbr...@gmail.com>
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.

radhames brito

unread,
Sep 27, 2010, 11:45:02 AM9/27/10
to rubyonra...@googlegroups.com
refer to them in the create action like i showed , no need to create action in the controller just the template in the views folder

radhames brito

unread,
Sep 27, 2010, 11:43:59 AM9/27/10
to rubyonra...@googlegroups.com

sure , add the corresponding logic to the failure.js.erb
Reply all
Reply to author
Forward
0 new messages