【问题标题】:Rails remote forms validationRails 远程表单验证
【发布时间】:2012-11-05 23:49:51
【问题描述】:

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

remote true 的工作方式很好,但我如何验证模型?

我有这个

<%= 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 %>

我如何处理错误?

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

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

【问题讨论】:

  • if @course.save 行将导致验证运行。什么不工作?

标签: ruby-on-rails ajax ruby-on-rails-3 validation


【解决方案1】:

由于您的 format.js 块是空的,并且创建操作是通过 :remote=>true 调用的,因此 rails 将执行默认操作,即查找 create.js.erb 以返回,它可能不是t 发现。

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

alert('HI');

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

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        

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

【讨论】:

  • +1。我注意到了同样的问题,但我认为更好的建议是使用浏览器调试器工具,如 chrome 开发者工具。如果发生这样的事情,它会立即返回错误。过去为我节省了大量时间。
  • O 这可行,但我的创建方法显示了用于创建模型的表单。即使我将@course errors_any 放在表单视图上,创建失败时也不会显示错误。 (是的,我已经验证了模型 w 验证了存在和 bla bla)。所以它有一种方法可以做类似 format.js { render :js=>@course.errors.full_messages } 的事情。谢谢
【解决方案2】:

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

 class Course 
    validates :title, :presence => true
 end

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

来恢复错误
    @course.errors.full_messages 

一旦你处理了这些错误,你就传递给视图并向用户显示一条消息。不记得具体如何使用格式 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);
});

});

【讨论】:

  • 我已经在模型上定义了验证,问题是当创建失败并且我正在执行远程操作时:是的,表单不显示错误,我将 logyc 用于显示它们跨度>
  • 禁止保存该课程:

  • 伙计,如果您使用远程,视图中的@course 将不保存任何内容,您无权访问该变量。您必须将该错误传递给您的 javascript CALLBACK 。
【解决方案3】:

我为此目的开发了 jquery 插件。可能值得一看。

https://github.com/bighostkim/remoteValidation

【讨论】:

    猜你喜欢
    • 2016-04-17
    • 2015-05-17
    • 2010-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-19
    • 1970-01-01
    • 2011-09-18
    相关资源
    最近更新 更多