weixin_33701251 2012-11-05 23:49 采纳率: 0%
浏览 20

Rails远程表单验证

Just a simple question, I have followed the railscast

It's nice how for remote true works, but how can I validate the model ?

I have this one

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

How I handle the error?

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

Any ideas why this does not work?

  • 写回答

3条回答 默认 最新

  • weixin_33738555 2012-11-06 00:04
    关注

    you have to define the validators in your model.

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

    In your controller, you have try to save the instance with @course.save . If it does not save, you get back the errors by calling

        @course.errors.full_messages 
    

    Once you get the hands in these errors , you pass to the view and display a message to the user. don't remember exacly how to do it with format js .

    EDIT 2 ======== check this link: http://www.alfajango.com/blog/rails-3-remote-links-and-forms/ try this

        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: 
    <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);
    });
    
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名