2017-08-15 49 views
2

我在單個控制器中有多個操作,我想在我的一個操作中重用一些代碼。所以我決定把它放在模型類的方法中。不幸的是,返回的變量不能在視圖中訪問。在這裏,您可以看到控制器動作(模型的方法我打電話是check_bibip_prices)Ruby on Rails:重複使用其他多個操作的代碼

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 

型號可以如下所示:

class Soldcar < ApplicationRecord 
    belongs_to :bibipprice 

    scope :isongoingauction, ->(isongoingauction) { where 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 do |car| 
     if (car.paintbodydamage < 10) and (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) and (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 

回答

2

的問題是,你叫render方法後,實例變量。

render :indexsold_to_winner方法的底部,它應該工作:

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