Partial rendering based on condition and sourced from different array

67 Views Asked by At

A somewhat complex action assembles two set of arrays into a single one.

@packages = (@packageoffers + @availables).sort_by(&:cost).paginate :page => params[:page], :per_page => 20

The view then renders a partial

@packages.each do |package|
  if @packages_m.include?(package)
    @mss = true
    render partial: 'package', collection: @packages
  elsif @availables.include?(package)
     @mss = false
     render partial: 'package', collection: @packages
  else
  end

Said partial then has two conditional block based on @mss (html code stripped):

number_to_currency(markup_price)
if @mss == true
  package.typounit.stars
else
  package.product_category

The error that occurs is occasional

ActionView::Template::Error (undefined method 'product_category' for #<Packageoffer:

Occasional, as the pagination has given a position via triangulation: pages where all results are from one array or the other render. The pages where the results are from both arrays generate the error.

While I can see the behaviour, I fail to comprehend why it functions when the collection comes from the same class.

And thus what is the solution? I see two possibilities:
• Can the partial call the sub array (@packageoffersor @availables)?
• is the boolean @mssa possible culprit?
• pagination can then become an issue as rails can lose track of which collection to count

1

There are 1 best solutions below

0
Maksim P On

I guess a problem is that you render collection https://api.rubyonrails.org/v5.0.7/classes/ActionView/PartialRenderer.html

I hope it can help you

  if @packages_m.include?(package)
    render partial: 'package', locals: { package: package, mss: true }
  elsif @availables.include?(package)
     render partial: 'package', locals: { package: package, mss: false }
  else
  end

  if mss
    package.typounit.stars
  else
    package.product_category