问题
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
View
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" %>
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.
Controller
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
Error This does not occur when a start_masterlocation_id is present in the database.
undefined method `name' for nil:NilClass
回答1:
I solved this by ignoring the nil problem. Instead, I used
!@newsavedmap.start_masterlocation_id.blank?
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.
来源:https://stackoverflow.com/questions/18603320/why-isnt-rails-recognizing-nil-if-blank-in-my-form