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
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:
This assumes that your controller has the following or something similar:
Hopefully this is enough to get you started. If not, some controller code will be required to help further.