Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 2: How can I get the return object of a method and print it on a view?
    primarykey
    data
    text
    <p>I developed hours ago a fuzzy method on <code>store_controller.rb</code>. This method, named <code>fuzzy</code>, gets the product quantity as argument, then, with if statements, checks the value and returns an object named <code>disp</code>. I call this method on the view <code>index.html.erb</code>.</p> <p>Now, in the view, I inserted a line that checks the value of <code>disp</code>. By the value of <code>disp</code>, the view must print out a different image that displays the available quantities of product, for example, <code>available</code> or <code>not available</code>. But, inserting this line, I got an error: <code>undefined local variable or method 'disp' for #&lt;ActionView::Base:0x6dc1864&gt;</code></p> <p>This is the code of the view <code>index.html.erb</code>:</p> <pre><code>&lt;% for product in @products -%&gt; &lt;div class="entry"&gt; &lt;%= image_tag(product.image_url) %&gt; &lt;h3&gt;&lt;%=h product.title %&gt;&lt;/h3&gt; &lt;%= product.descr %&gt; &lt;br /&gt;&lt;div class="disp"&gt; &lt;p&gt;&lt;% fuzzy(product.quantity) %&gt;&lt;% if disp == 0 %&gt;&lt;%= image_tag("nodisp.png", :border =&gt; "0") %&gt;&lt;% end %&gt;&lt;% if disp == 1 %&gt;&lt;%= image_tag("disp.png", :border =&gt; "0") %&gt;&lt;% end %&gt;&lt;/p&gt; &lt;/div&gt;&lt;br /&gt; &lt;p&gt;&lt;span class="price"&gt;&lt;%= number_to_currency(product.price, :unit =&gt; "&amp;euro;") %&gt;&lt;/span&gt;&lt;/p&gt; &lt;% form_remote_tag :url =&gt; { :action =&gt; :add_to_cart, :id =&gt; product } do %&gt; &lt;%= submit_tag "add to cart!" %&gt; &lt;% end %&gt; &lt;/div&gt; &lt;% end %&gt; </code></pre> <p>Please notice the call of method <code>fuzzy</code>. This method is inserted in <code>store_controller.rb</code>:</p> <pre><code>def fuzzy(q) disp = (q-5)/20 if q &gt; 9 then disp = 1 end if q &lt; 7 then disp = 0 end return disp end </code></pre> <p>The question is: When I execute the code in the view, calling fuzzy method, how I can get the result <code>disp</code> of the method, and, with if statements, diplay it on the view? Is it possible?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload