Why isn't Rails recognizing nil if blank in my form? -
i'm reworking rails 2 website. right now, i'm getting error, , think it's because value being submitted blank instead of .nil. however, attempts keep nil don't seem working. appreciate have 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
view
i've got jquery adds value hidden field when start_masterlocation_id exists. i've got jquery removes field attribute when not exist.
<%= hidden_field :newsavedmap, :start_masterlocation_id, :id => "start-masterlocation-id-field" %>
finally, here part of controller throwing error. controller page holds form (maptry), not controller newsavedmap. think have delete @newsavedmap.id, @newsavedmap.mapname, , @newsavedmap.optimize lines i'm going form handlers, don't think that's related error.
controller
def maptry @itinerary = itinerary.find(params[:id]) if @itinerary.user_id == current_user.id respond_to |format| @masterlocation = masterlocation.find(:all) format.html end format.xml { render :xml => @masterlocation } format.js @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
error not occur when start_masterlocation_id present in database.
undefined method `name' nil:nilclass
i solved ignoring nil problem. instead, used
!@newsavedmap.start_masterlocation_id.blank?
this evaluates whether data in record blank , either or doesn't something. obviously, results in unwanted "blank" data being left inside database, it's not ideal, helped me move forward project. i'd appreciate responses deal directly nil issue , tell me how have rails ignore blank data in form.
Comments
Post a Comment