ruby on rails - Unable to update attribute - undefined method -
missing fundamental here. unable update items_loaded
once rest client done fetching items api.
live app can run on fly: http://runnable.com/vw9rqx-kiiffmpii/ajax-affiliates
undefined method `items_loaded=' #<class:0x000000037cce20> app/models/affiliate.rb:17:in `set_items_loaded' app/controllers/main_controller.rb:8:in `index'
main_controller.rb
class maincontroller < applicationcontroller def index # delay fetching # @products = affiliate.fetch @products = affiliate.delay.fetch # let know when fetching done affiliate.set_items_loaded end def check_items_loaded @items_status = affiliate.items_loaded respond_to |wants| wants.js end end end
affiliate.rb
require "rest_client" class affiliate < activerecord::base def self.fetch response = restclient::request.execute( :method => :get, :url => "http://api.shopstyle.com/api/v2/products?pid=uid7849-6112293-28&fts=women&offset=0&limit=10" ) @products = json.parse(response)["products"].map |product| product = openstruct.new(product) product end end def self.set_items_loaded self.items_loaded = true end end
20150604120114_add_items_loaded_to_affiliates.rb
class additemsloadedtoaffiliates < activerecord::migration def self.up change_table :affiliates |t| t.column :items_loaded, :boolean, default: false end end def self.down change_table :affiliates |t| t.remove :items_loaded end end end
actually, in class affiliate, defined method self.set_items_loaded affiliate object , set attribute items_loaded true on each object of class.
if want that, should write
affiliate.rb
def self.set_items_loaded self.update_all(items_loaded: true) end
main_controller.rb
affiliate.set_items_loaded
if want update 1 object of affiliate set item_loaded true, should define method way , use on 1 object
affiliate.rb
def set_items_loaded self.items_loaded = true end
main_controller.rb
affiliate.first.set_items_loaded # first object of affiliate updated
Comments
Post a Comment