如果我的表单中有空白,Rails为什么不能识别nil? [英] Why isn't Rails recognizing nil if blank in my form?

查看:103
本文介绍了如果我的表单中有空白,Rails为什么不能识别nil?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在重新构建Rails 2网站.现在,我遇到了一个错误,我认为这是因为一个值被提交为空白而不是.nil.但是,我试图使其保持为零的尝试似乎没有用.感谢您提供的任何帮助.

I'm reworking a Rails 2 website. Right now, I'm getting an error, and I think it's because a value is being submitted as blank instead of .nil. However, my attempts to keep it nil don't seem to be working. I appreciate any help you have to offer.

从模型中,基于使空白参数[]无效

From Model, based on Make blank params[] nil

    NULL_ATTRS = %w( start_masterlocation_id )
    before_save :nil_if_blank

    protected

    def nil_if_blank
    NULL_ATTRS.each { |attr| self[attr] = nil if self[attr].blank? }
    end

查看

我有一个jQuery,当存在一个start_masterlocation_id时,它会向该隐藏字段添加一个值.我还有一个jQuery,它在不存在时删除了field属性.

I've got jQuery that adds a value to this hidden field when a start_masterlocation_id exists. I've also got jQuery that removes the field attribute when it does not exist.

    <%= hidden_field :newsavedmap, :start_masterlocation_id, :id => "start-masterlocation-id-field" %>

最后,这是引发错误的控制器部分.这是保存表单(Maptry)的页面的控制器,而不是Newsavedmap的控制器.我想我现在要使用表单处理程序时,必须删除@ newsavedmap.id,@ newsavedmap.mapname和@ newsavedmap.optimize行,但是我认为这与此错误无关.

Finally, here is the part of the controller that is throwing the error. This is the controller for the page that holds the form (Maptry), not the controller for Newsavedmap. I think I have to delete the @newsavedmap.id, @newsavedmap.mapname, and @newsavedmap.optimize lines now that I'm going with form handlers, but I don't think that's related to this error.

控制器

    def maptry
    @itinerary = Itinerary.find(params[:id])
    if @itinerary.user_id == current_user.id
    respond_to do |format|
    @masterlocation = Masterlocation.find(:all)
    format.html do
    end
    format.xml  { render :xml => @masterlocation }
    format.js do
    @masterlocation = Masterlocation.find(:all)
    render :json => @masterlocation.to_json(:only => [:id, :nickname, :latitude, :longitude])
    end
    end
    if params[:newsavedmap_id]
    @newsavedmap = Newsavedmap.find(:first, :conditions => {:id => params[:newsavedmap_id]})
    @waypoint = Waypoint.find(:all, :conditions => {:newsavedmap_id => params[:newsavedmap_id]})
    @newsavedmap.id = params[:newsavedmap_id]
    @newsavedmap.mapname = Newsavedmap.find(:first, :conditions => {:id => params[:newsavedmap_id]}).mapname
    @newsavedmap.optimize = Newsavedmap.find(:first, :conditions => {:id => params[:newsavedmap_id]}).optimize
    if !@newsavedmap.start_masterlocation_id.nil?       
    @start_name = Masterlocation.find(:first, :conditions => {:id => @newsavedmap.start_masterlocation_id}).name
    end
    if !@newsavedmap.end_masterlocation_id.nil?     
    @end_name = Masterlocation.find(:first, :conditions => {:id => @newsavedmap.end_masterlocation_id}).name
    end
    else
    @newsavedmap = Newsavedmap.new  
    end
    else
    redirect_to '/'
    end
    end

错误 如果数据库中存在start_masterlocation_id,则不会发生这种情况.

Error This does not occur when a start_masterlocation_id is present in the database.

    undefined method `name' for nil:NilClass

推荐答案

我通过忽略nil问题解决了这一问题.相反,我使用

I solved this by ignoring the nil problem. Instead, I used

    !@newsavedmap.start_masterlocation_id.blank? 

这将评估记录中的数据是否为空白,然后执行或不执行某项操作.显然,这导致不必要的空白"数据保留在数据库中,因此并不理想,但是它帮助我推进了该项目.我希望您能直接解决nil问题,并告诉我如何让Rails忽略表单中的空白数据.

This evaluates whether the data in the record is blank and then either does or doesn't do something. Obviously, this results in unwanted "blank" data being left inside my database, so it's not ideal, but it helped me move forward with the project. I'd appreciate any responses that deal directly with the nil issue and tell me how to have Rails ignore blank data in a form.

这篇关于如果我的表单中有空白,Rails为什么不能识别nil?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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