ruby on rails: controller can't access newly added record -
i'm quite new ror. @ first used sqlite3 database migrated mysql. worked fine application until added record database.
i can update new records using "irb" console. have function in controller called python script updates record.
here's function:
# /dd_bots/update # param name, att def update obj = object.all obj = obj.select{ |o| o.name == params[:name] } obj[0].update_attribute(:att, params[:att]) head :ok end however function doesn't update newly added records returning error: nomethoderror (undefined method update_attribute' nil:nilclass)
obviously record doesn't seem found...
do have clue why happening?
firstly, don't call model object: have disastrous consequences, object base class practically every object in ruby.
secondly, update_attribute being called on first element of obj, array. error if array contains no elements, i.e. if there no objects name same name parameter being passed controller.
you can in way that's less error prone:
def update obj = object.find_by_name(params[:name]) obj.update_attribute(:att, params[:att]) if obj head :ok end if record should exist, might throw exception if doesn't. can adding ! find_by_name method:
obj = object.find_by_name!(params[:name]) # throw activerecord::recordnotfound this has benefit of giving http 404 status code if record doesn't exist, acts in same way otherwise.
Comments
Post a Comment