0

I did a simple rails form with two user inputs , name and email. I'm using rails 3.1, so I used :remote => true in my form_for tag. I also created a create.js.erb and added corresponding format.js in the users_controller.rb.

I added the following lines in create.js.erb

$(document).ready(function(){ $('body').html("<h1>Registration Successful</h1>") }); 

Now the problem is that when I click the submit button, with empty fields, it does not show validations error messages that I have added in the user.rb model file. Instead, it will show me registration successful message.

Is there some easy way to circumvent this problems in rails. Also please note that I'm very bad at javascript, so please be nice to me.

controller code for create

 def create @user = User.new(params[:user]) respond_to do |format| if @user.save #UserMailer.registration_confirmation(@user).deliver format.html { redirect_to @user, notice: 'User was successfully created.' } format.js else format.html { render action: "new" } format.js end end end 
1
  • Show us your controller code.
    – rwilliams
    CommentedSep 25, 2011 at 21:34

1 Answer 1

1

The reason it doesn't show the errors is because you are not telling it to, you are simply telling it to write "Registration Successful". You could try:

<% @user.errors.full_messages.each do |error| %> $('body').prepend("<h1><%=error%></h1>"); <% end %> 

This assumes that your controller has the following or something similar:

@user = User.create(params[:user]) 

Hopefully this is enough to get you started. If not, some controller code will be required to help further.

3
  • Is it not possible, that once the submit button is clicked, before submitting the form, the validations error shows, the rails way
    – Nikhil
    CommentedSep 25, 2011 at 22:54
  • You will want to validate both the client and the server. There is a gem that helps combine the two though, check out the client_side_validations gem. github.com/bcardarella/client_side_validations
    – Gazler
    CommentedSep 25, 2011 at 23:00
  • OMG !! I think I saw this gem in one of the railcasts screencast. Thank you for reminding me.
    – Nikhil
    CommentedSep 25, 2011 at 23:06

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.