Rails远程表单验证

Non*_*yck 5 validation ajax ruby-on-rails ruby-on-rails-3

只是一个简单的问题,我已经关注了railscast

对于远程真实工作来说,这很好,但我如何验证模型呢?

我有这个

<%= form_for [:admin,@course], remote: true  do |f| %>
  <div class="field">
    <%= f.label :title, "The title:" %><br />
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :description, "Descripcion:" %><br />
    <%= f.text_area :description %>
 </div>
 <div class="actions">
   <%= f.submit "Send"%>
 </div>
<% end %>
Run Code Online (Sandbox Code Playgroud)

我如何处理错误?

def create

  @course = Course.new(params[:course])

  respond_to do |format|
    if @course.save
      format.html { redirect_to admin_index_url, :notice=> 'Done !!' }
      format.js
    else
      format.html { flash.now[:alert] = "Not done" 
                    render "new"
                  }
      format.js
    end
  end        
end
Run Code Online (Sandbox Code Playgroud)

任何想法为什么这不起作用?

小智 6

由于您的format.js块为空,并且通过以下方式调用create动作:remote => true,rails将执行默认操作,即查找create.js.erb以进行回放,这可能是找不到的.

为了说明,将它放在app/views /?/ create.js.erb中的views目录中:

alert('HI');
Run Code Online (Sandbox Code Playgroud)

然后将其放入您的控制器中以查看替代方案:

respond_to do |format|
  if @course.save
    format.html { redirect_to admin_index_url, :notice=> 'Done !!' }
    format.js { render :js=>'alert("done");' }
  else
    format.html { flash.now[:alert] = "Not done" 
                render "new"
                }
    format.js { render :js=>'alert("not done");' }
  end
end        
Run Code Online (Sandbox Code Playgroud)

所以现在,您只需要将错误消息发送回HTML/Javascript,最可能的方法是将其放入相应的.js.erb文件中.


Rod*_*ias 2

您必须在模型中定义验证器。

 class Course 
    validates :title, :presence => true
 end
Run Code Online (Sandbox Code Playgroud)

在您的控制器中,您尝试使用 @course.save 保存实例。如果没有保存,您可以通过调用返回错误

    @course.errors.full_messages 
Run Code Online (Sandbox Code Playgroud)

一旦掌握了这些错误,您就可以传递到视图并向用户显示一条消息。不记得确切如何使用 format js 来做到这一点。

编辑2 ======== 检查此链接:http://www.alfajango.com/blog/rails-3-remote-links-and-forms/ 试试这个

    respond_to do |format|
       if @course.save
          format.html { redirect_to admin_index_url, :notice=> 'Done !!' }
          format.js { render :js=>'alert("done");' }
       else
         render :json => @comment.errors, :status => :unprocessable_entity
       end
    end  


 In your javascript, bind the error callback and get the errors you passed in your controller.


 $(document).ready(function(){

$('#form_name').bind("ajax:error", function(evt, xhr, status, error){
  var $form = $(this),
      errors,
      errorText;

  try {
    // Populate errorText with the comment errors
    errors = $.parseJSON(xhr.responseText);
  } catch(err) {
    // If the responseText is not valid JSON (like if a 500 exception was thrown), populate errors with a generic error message.
    errors = {message: "Please reload the page and try again"};
  }

  // Build an unordered list from the list of errors
  errorText = "There were errors with the submission: \n<ul>";

  for ( error in errors ) {
    errorText += "<li>" + error + ': ' + errors[error] + "</li> ";
  }

  errorText += "</ul>";

  // Insert error list into form
  $form.find('div.validation-error').html(errorText);
});

});
Run Code Online (Sandbox Code Playgroud)