错误在Rails 3的处理与阿贾克斯 [英] Error Handling with Ajax in Rails 3

查看:137
本文介绍了错误在Rails 3的处理与阿贾克斯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建一个简单的演示应用程序,允许用户输入他们的电子邮件地址来注册自己在接受测试的访问兴趣。该应用程序,然后将它们发送确认电子邮件,让他们知道我们已经收到了他们的请求。如果你曾经签署了一个测试版的发布得到通知,然后你的想法。

I'm creating a simple demo app that allows a user to enter their email address to register their interest in receiving beta access. The app then sends them a confirmation email that lets them know we've received their request. If you've ever signed up to be notified of a beta launch then you get the idea.

我很好奇如何同时使用AJAX在Rails 3中处理错误。实施我的respond_to块之前,我有这样的呈现共享的错误形式的部分。

I'm curious about how to handle errors in Rails 3 while using AJAX. Before implementing my respond_to block I had a form that rendered a shared errors partial.

下面的表格。

<% if flash[:notice] %>
<p><%= flash[:notice] %></p>
<% end %>

<p>Sign up to be notified when the beta launches.</p>

<%= form_for @user, :remote => true do |form| %>

    <%= render '/shared/errors', :target => @user %>

    <%= form.label :email, "Your Email Address" %>
    <%= form.text_field :email %>

    <%= form.submit "Notify Me" %>
<% end %>

下面是部分上述错误。

And here's the aforementioned errors partial.

<% if target.errors.any? %>

<ul>
    <% target.errors.full_messages.each do |message| %>
        <li><%= message %></li>
    <% end %>
</ul>

<% end %>

非常标准的东西。该控制器的动作看起来是这样的。

Very standard stuff. The controller action looks like this.

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

respond_to do |format|
  if @user.save
    format.html { redirect_to :back, flash[:notice] = "Thanks for your interest! We'll let you know when the app is in beta." }
    format.js
  else
    format.html { render :action => :new }
    format.js
  end
end
end

一切完美实现Ajax之前。如果表单通过验证,然后他们看到了成功的闪光消息,如果没有的话,他们看到错误的列表。所以,现在我有一个create.js.erb文件,我应该怎么处理这些错误不重复自己或者是不可能的。我当然想保持这种尽可能的干燥。

Everything works perfectly before implementing ajax. If the form passes validation then they see the success flash message and if not then they see a list of errors. So now that i have a create.js.erb file how should I handle the errors without repeating myself or is that impossible. I obviously want to keep this as DRY as possible.

推荐答案

您仍然可以呈现一个共享部分的所有.js文件中的错误js.erb文件。

You can still render a shared partial for all .js errors in your js.erb file.

<% if @user.errors.any? %>
   var el = $('#create_user_form');
   // Create a list of errors
   <%= render :partial=>'js_errors', :locals=>{:target=> @user} %>
 <% else %>
     $('#users_list').append("<%= escape_javascript(render :partial=>"users/show", :locals=>{:user => @user }) %>");
    // Clear form
    el.find('input:text,textarea').val('');
    el.find('.validation-errors').empty();
<% end %>

和你的部分可能看起来像(假设jQuery的):

And your partial could look like (Assuming jquery):

  <% target.errors.full_messages.each do |error| %>
    var errors = $('<ul />');
    errors.append('<li><%= escape_javascript( error ) %></li>');
  <% end %>

但也有另一种选择......它甚至机。

But there's also ANOTHER option...It's even DRYer.

<一个href="http://www.alfajango.com/blog/rails-3-remote-links-and-forms/">http://www.alfajango.com/blog/rails-3-remote-links-and-forms/

如果您正在通过自己的方式阿贾克斯轨道3,本指南确实是最好的了解响应和AJAX渲染,因为它目前的立场。

If you are working your way through ajax in rails 3, this guide is really the best for understanding responses and ajax rendering as it currently stands.

我的工作,通过本指南,并张贴在你怎么能实际使用的HTML谐音为HTML和AJAX请求响应的意见。我做到了意外,然后跟进如何做到这一点。

I worked through this guide and posted in the comments how you can actually use your HTML partials for both HTML and AJAX request responses. I did it by accident and then followed up on how to do it.

享受!

实际上,你可以返回直线上升的HTML与就像在你的反应。

You can actually return straight-up html with your response just like before.

这里的短版:

def create
     @something = Somethng.new(params[:something])
     if @something.save
       respond_with( @something, :status => :created, :location => @something ) do |format|
         format.html do
           if request.xhr?
             render :partial => "something/show", :locals => { :billable => @billable }, :layout => false
           end
         end
       end
       else
        respond_with( @something.errors, :status => :unprocessable_entity ) do |format|
          format.html do
            if request.xhr?
              render :partial => "something/new", :locals => { :something => @something }, :layout => false
            else
              render :action => :new
            end
          end
        end
      end
    end

这篇关于错误在Rails 3的处理与阿贾克斯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆