Ruby on Rails: reuse code across other multiple actions -
i have multiple actions within single controller , want reuse of codes in 1 of actions. decided put in model class method. unfortunately, returned variables can not accessed in view. here can see controller action (model method calling check_bibip_prices)
def sold_to_winner @soldcars = soldcar.winner_username(params[:winnerusername]).order("auctioncarenddate desc").paginate(:page => params[:page], :per_page => 15) @page_name = "cars sold to: " + params[:winnerusername] render :index authorize! :show, @soldcars @car_buy_prices, @car_sell_prices, @car_status, @car_salesprice_background, @car_winnersalesprice_background = soldcar.check_bibip_prices(@soldcars) end model can seen follows:
class soldcar < applicationrecord belongs_to :bibipprice scope :isongoingauction, ->(isongoingauction) { isongoingauction: isongoingauction } scope :winner_username, ->(winnerusername) { where(winnerusername: winnerusername) } def self.check_bibip_prices(soldcars) @car_buy_prices = {} @car_sell_prices = {} @car_status = {} @car_salesprice_background = {} @car_winnersalesprice_background = {} @soldcars.each |car| if (car.paintbodydamage < 10) , (car.mechanicaldamage < 2) @car_status[car.carid] = "excellent" @car_buy_prices[car.carid] = car.bibipprice.dealer_price_three @car_sell_prices[car.carid] = car.bibipprice.estimated_price_three elsif (car.paintbodydamage < 18) , (car.mechanicaldamage < 5) @car_status[car.carid] = "good" @car_buy_prices[car.carid] = car.bibipprice.dealer_price_two @car_sell_prices[car.carid] = car.bibipprice.estimated_price_two else @car_status[car.carid] = "fair" @car_buy_prices[car.carid] = car.bibipprice.dealer_price_one @car_sell_prices[car.carid] = car.bibipprice.estimated_price_one end if car.bibipprice.estimated_price_three > 0 if car.winnersalesprice > @car_sell_prices[car.carid] @car_winnersalesprice_background[car.carid] = "pricealert" elsif car.winnersalesprice > @car_buy_prices[car.carid] @car_winnersalesprice_background[car.carid] = "pricesoso" elsif car.winnersalesprice > 0 @car_winnersalesprice_background[car.carid] = "priceok" end if car.salesprice > @car_sell_prices[car.carid] @car_salesprice_background[car.carid] = "pricealert" elsif car.salesprice > @car_buy_prices[car.carid] @car_salesprice_background[car.carid] = "pricesoso" else @car_salesprice_background[car.carid] = "priceok" end else @car_salesprice_background[car.carid] = "" @car_winnersalesprice_background[car.carid] = "" end end return @soldcarscar_buy_prices, @soldcarscar_sell_prices, @soldcarscar_status, @soldcarscar_salesprice_background, @soldcarscar_winnersalesprice_background end end
the problem instantiate variables after calling render method.
put render :index @ bottom of sold_to_winner method , should work:
def sold_to_winner @soldcars = soldcar.winner_username(params[:winnerusername]).order("auctioncarenddate desc").paginate(:page => params[:page], :per_page => 15) @page_name = "cars sold to: " + params[:winnerusername] authorize! :show, @soldcars @car_buy_prices, @car_sell_prices, @car_status, @car_salesprice_background, @car_winnersalesprice_background = soldcar.check_bibip_prices(@soldcars) render :index end
Comments
Post a Comment