render error messages with js form rails - ruby-on-rails

I changed my form to remote, and while the form now works, the error messages are no longer displaying if there's an error.
<%= render 'shared/error_messages' %>
is there a good wear to get the messages to show again?
below is my controller...
thank you.
respond_to do |format|
if #post.save
format.js { render :js => "window.location = '#{edit_post_path #post}'" }
format.html { redirect_to [:edit, #post] }
else
format.js { render :js => #post.errors }
format.html { redirect_to '/', :error => "Could not save comment" }
end
end

respond_to do |format|
if #post.save
format.js { render :js => "window.location = '#{edit_post_path #post}'" }
format.html { redirect_to [:edit, #post] }
else
format.js { }
format.html { redirect_to '/', :error => "Could not save comment" }
end
end
# update.js.erb
$(document).find("form").prepend('<%= escape_javascript(render("shared/error_messages", :formats => [:html])) %>');

Related

Append recaptcha message error to rails errors.messages.values

I have such code in controller:
if verify_recaptcha #verify_recaptcha(:model => #order, :message => "Oh! It's error with reCAPTCHA!") &&
respond_to do |format|
if #order.save
Cart.destroy(session[:cart_id])
session[:cart_id] = nil
OrderMailer.order_confirmation(#order, #user).deliver
UserOrderMailer.user_order_mailer(#order, #user).deliver
#OrderNotifier.received(#order).deliver
pdf_link = generate_pdf(#order)
link = " #{I18n.t(:get_pdf)}"
format.html { redirect_to root_url, :notice => (I18n.t(:successful_order_send)+link.to_s).html_safe}
else
format.html { render :action => "new", :notice => I18n.t(:error) }
format.xml { render :xml => #order.errors, :status => :unprocessable_entity }
end
end
else
flash[:warning] = I18n.t(:wrong_captcha)
redirect_to :back
end
And in form view:
- if #user_vehicle.errors.messages.values.present?
.warning
- #user_vehicle.errors.messages.values.each do |msg|
- msg.each do |m|
%li= m
But also in layout i have:
- if flash[:warning]
.warning
= flash[:warning]
- if flash[:notice]
.notice
= flash[:notice]
I want to know, how can i append recaptcha fail error to errors.messages.values list of error's and display in same div and with li as i do for model validation messages?
How can i see as one more li item recaptcha message?
if verify_recaptcha
...
else
#user_vehicle.errors.add(:base, I18n.t(:wrong_captcha))
end

Rails 3 - Redirect_to / Render another controller

I have 2 controllers: Projects and Users. Both models have no relation at all.
When I create a new Project I want to redirect to the new User path after saving the new project, but all my tries give erros like missing template or stuff like that.
How can I get this working?
EDITED
My create method in Projects controller:
def create
#project = Project.new(params[:project])
respond_to do |format|
if #project.save
format.html { render (new_user_registration_path) }
else
format.html { render :action => "new" }
format.xml { render :xml => #project.errors, :status => :unprocessable_entity }
end
end
end
You don't want to render new_user_registration_path, you want to redirect_to new_user_registration_path
you must use redirect_to instead render:
redirect_to new_user_registration_path
respond_to do |format|
if #project.save
format.html { redirect_to new_user_registration_path }
else
format.html { render :action => "new" }
format.xml { render :xml => #project.errors, :status => :unprocessable_entity }
end
end

Where should I place my respond_to block?

Lets say I have an ArtcilesController with create action like following.
def create
#article = Article.new(params[:article])
respond_to do |format|
if #article.save
format.html { redirect_to(#article, :notice => "Article created") }
format.json { render :show }
else
format.html { render :new }
format.json { render(:json => { :errors => #article.errors }, :status => :not_acceptable) }
end
end
end
The same action can be written like following also:
def create
#article = Article.new(params[:article])
if #article.save
respond_to do |format|
format.html { redirect_to(#article, :notice => "Article created") }
format.json { render :show }
end
else
respond_to do |format|
format.html { render :new }
format.json { render(:json => { :errors => #article.errors }, :status => :not_acceptable) }
end
end
end
Notice that in first example there is an if else block inside of a respond_to block and in second, there are two respond_to blocks inside of a single if else block.
Should I prefer one over other? If yes, any reasons why? Or is it just a matter of choosing a style and sticking with it?
Style only however you're only ever responding to one request and using routing logic in your controller according to your models.
def create
#article = Article.new(params[:article])
respond_to do |format|
format.html {
#article.save ? redirect_to(#article, :notice => "Article created") : render :new
}
format.json {
#article.save ? render(:show) : render(:json => { :errors => #article.errors }, :status => :not_acceptable)
}
end
end
respond_with in Rails 3 will DRY this up, for boilerplate code as above. It even handles rendering edit/new forms and error messages when validation fails.

swfupload 406 not acceptable error

def create
#asset = Asset.new(params[:asset])
respond_to do |format|
if params[:Filedata]
#asset = Asset.new :swf_uploaded_data => params[:Filedata]
#asset.user = current_user
#asset.save!
format.html { render :text => #asset.image.url(:thumb) }
format.xml { render :nothing => true }
else
if #asset.save
flash[:notice] = 'Created'
format.html { redirect_to(#asset) }
format.xml { render :xml => #asset, :status => :created, :location => #asset }
else
format.html { render :action => "new" }
format.xml { render :xml => #asset.errors, :status => :unprocessable_entity }
end
end
end
end
I am creating an upload status bar with swfupload. At the end of the upload I get a 406 error.
Not necessarily your answer but I had the same problem with Plupload and it happens that the request format is nil.
It might not be the best way but one could do the following:
request.format ||= :xml
in order to provide some default format in the method. Hope it can help others.

Missing templates from new Rails 3 app?

Just tried writing a simple validates_presence_of in my model, and when the errors try to render, it calls this :
Template is missing
Missing template posts/create with {:locale=>[:en, :en], :handlers=>[:builder, :rjs, :erb, :rhtml, :rxml, :haml], :formats=>[:html]} in view paths "/Users/johnsmith/Sites/shwagr/app/views"
Errors don't have seperate views in Rails3 do they? I thought that was Rails magic..
Curious if anyone had this problem, or knew how to make it correctly validate.
My Model:
validates_presence_of :category, :name, :url
My Controller:
def new
#post = Post.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #post }
end
end
def create
#post = Post.new(params[:post])
if #post.valid? && current_user.posts << #post
respond_to do |format|
if #post.save
format.html { redirect_to(#post, :notice => 'Post was successfully created.') }
format.xml { render :xml => #post, :status => :created, :location => #post }
else
format.html { render :action => "new" }
format.xml { render :xml => #post.errors, :status => :unprocessable_entity }
end
end
end
end
Update
Interesting, I 'touch app/views/posts/create.html.haml', and now it removed the error and instead loads that page instead. But why would it? Or more importantly, how can I make it just redirect back to the new_post_path(#post) like it should?
If your
if #post.valid? && current_user.posts << #post
line returns false, no render() or redirect_to() is called. Rails' default behavior then is to render the view with the same name as your method. That would be create.BUILDER.FORMAT.
Try to remove the line. Use this code instead:
#post = current_user.posts.new(params[:post])
respond_to do |format|
if #post.save
...
Or write an else case with
render :action => "new"
Ah got it. This is because it was never valid so it would loop back to itself on 'create', find no template there and error out. The correct way to set up the def create would be this
def create
#post = Post.new(params[:post])
if #post.valid? && current_user.posts << #post
respond_to do |format|
if #post.save
format.html { redirect_to(#post, :notice => 'Post was successfully created.') }
format.xml { render :xml => #post, :status => :created, :location => #post }
else
format.html { redirect_to new_user_post_path(:current) }
format.xml { render :xml => #post.errors, :status => :unprocessable_entity }
end
end
else
render :action => 'new'
end
end
No, they don't have seperate views. So do you have a app/views/posts/create.html.erb file?

Resources