Tolga Tolga -3 years ago 99
Ruby Question

Ruby on Rails: reuse code across other multiple actions

I have multiple actions within a single controller and I want to reuse some of the codes in one of my actions. So I decided to put it in a model class method. Unfortunately, returned variables can not be accessed in the view. Here you can see the controller action (model method I am calling is 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 be seen as follows:

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

Answer Source

The problem is that you instantiate the variables after calling render method.

Put render :index at the bottom of the sold_to_winner method and it 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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download