0

the following view

<% @packageoffers.each do |packageoffer| %>
  <% if @packageoffers_mss.include?(packageoffer) %>
  <% elsif @availables.include?(packageoffer) %>
    <%= render 'packageoffer', collection: @packageoffers %>

is returning an NameError undefined local variable or method 'packageoffer' when trying to process the partial _packageoffer.html.erb upon a line like the second taht follows:

<% item_sale_price = @item_price.sale_price * params[:quantity].to_i %>
<% markup_price = ((item_sale_price + (packageoffer.price * @r * @q)) * (1 + (@user.markup.to_d / 100) ) )  %>

note: @item_price is itself an item of an array section and had to be declared as an instance variable to pass it along <% @item_price = @allotments.select{ |a| a.section_id == section.id }.first

Thus, notwithstanding following the rails documentation guidelines, I cannot process the array's item. Where lies the mistake?

5
  • Try <%= render partial: 'packageoffer', collection: @packageoffers %> or just <%= render @packageoffers %> Commented Sep 27, 2018 at 16:12
  • Waiter, bring me a second FULL pie of that humble stuff you got. The guideline does state render partial: The rote of calling render 'partial' is strong... Commented Sep 27, 2018 at 16:18
  • 1
    You mean that worked? Commented Sep 27, 2018 at 16:20
  • It seems like you're trying to render the entire collection, inside a loop over the collection. That means you'll render the whole collection many times. Are you trying to do that? Or do you just mean to render the collection once, with some items left out? If the later, you want <%= render packageoffer %>. Commented Sep 27, 2018 at 16:28
  • yes @Pavan. that worked! Commented Sep 27, 2018 at 16:49

2 Answers 2

2

You're using collection rendering incorrectly.

Your line...

<%= render 'packageoffer', collection: @packageoffers %>

Renders a single partial, and passes a local variable called collection with a value of @packageoffers.

The line you've written is a shorthand for:

<%= render partial: 'packageoffer', locals: { collection: @packageoffers } %>

If you want to render the entire collection and provide a partial name, you cannot use the render shorthand, you must use the explicit version of render partial:...:

<%= render partial: 'packageoffer', collection: @packageoffers %>

That said, it seems like your intent is to iterate over the collection, and conditionally render some items from it. In this case, you should not be using collection rendering at all, you should just use <%= render packageoffer %> to allow Rails to render the singular _packageoffer partial for each record.

Sign up to request clarification or add additional context in comments.

2 Comments

I was writing an answer and looked at yours,realized I was explaining why it didn't worked wrongly, aborted my answer.
I never would've gotten to that shorthand interpretation. While irrelevant to the question at hand, the longhand does lead to a different result (I believe, not certain given iterations of changes).
-1

Is the model called Packageoffer or PackageOffer? I think your problem is that Rails tries to infer the name of the variable from the model name. Can you try if there's a packge_offer?

If the model is PackageOffer, then you should use _package_offer as the partial name, package_offer as the variable name and also @package_offers. To be consistent with Rails conventions.

EDIT: if that's the problem, you can use your code with an as: :packageoffer option on the render to tell rails the name to use

1 Comment

useful thought regarding rails conventions. Admittedly I like the german language approach of hooking up wagons to make up a singular train with a specific meaning... and I tend to use that (the underscores are visually easier to digest, but the camelCasing on the class bothers me more - my set of rails, pun intended!). The class here was Packageoffer .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.