There is an example of an Ajax form in the Ch8 Beginning Rails book that works fine, except that it doesn't display a warning about invalid input.
Controller Code:
def create
@comment = @article.comments.new(params[:comment])
if @comment.save
respond_to do |format|
format.html { redirect_to @article, :notice => 'Thanks for your comment' }
format.js
end
else
respond_to do |format|
format.html { redirect_to @article, :alert => 'Unable to add comment due to errors in your input'}
format.js { render 'fail.js.erb' }
end
end
end
The file 'fail_create.js.erb' contains one line;
alert("<%= @comment.errors.full_messages.to_sentence %>");
Can some person explain why this is not working, thanks
source
share