2

I have @productrecords and @clone_record in controller method. While deploying the following code into staging,

= f.association :product, :collection => Category.products,:include_blank => "Select Product",:label => "* Product", :selected =>@productrecords.product_id ? @productrecords.product_id : @clone_record.product_id

I get an error "Avoid using instance variables in partials views" in houndci bot.

What is the best practice to fix this issue?

3
  • Where is this line located, in a partial? Commented Jan 30, 2018 at 6:29
  • you can render partial as <%= render partial: 'products', collection: Category.products, locals: {size: 30} %> Commented Jan 30, 2018 at 6:39
  • Yes. Its in a partial file Commented Jan 30, 2018 at 6:42

1 Answer 1

5

Yes. It is located in a partial file

That's what the warning/error tells you: don't use instance variables in partial files. Rather, use local variables. Example:

Your partial render looks something like this:

= render 'my_partial'

Change it like this to pass all variables explicitly:

= render 'my_partial', productrecords: @productrecords, clone_record: @clone_record

And your partial will use the locals now:

= f.association :product, :collection => Category.products,:include_blank => "Select Product",
               :label => "* Product", 
               :selected =>productrecords.product_id ? productrecords.product_id : clone_record.product_id
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you ji. Great

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.