使用Rails 3获取未定义的局部变量或方法错误

时间:2015-04-28 12:20:57

标签: ruby ruby-on-rails-3.2

您好,我在尝试使用Rails 3渲染表时收到以下错误。

  

错误:

ActionView::Template::Error (undefined local variable or method `result' for #<#
<Class:0x3460410>:0x2af4d10>):

我正在解释下面的代码片段。

  

payments_controller.rb:

class PaymentsController < ApplicationController

    def payment
        @payment=Vendor.new
        respond_to do |format|
            format.html 
            format.js
        end

    end
    def check_type  
        if params[:commit]=="submit"
            @vendor_type = PaymentVendor.where(:v_name => params[:v_name]).map{|v|[v.v_catagory ,v.Receipt_No]}
            #@vendor_type = PaymentVendor.where(:v_name => params[:v_name]).pluck(:v_catagory)
            output=[]
            result=[]
            @vendor_type.each do |i|
              if i.first == params[:payment][:s_catagory]
                output << i[1]
              end  
            end  
            output.each_with_index{|val, index|
               #puts "#{val} => #{index}" 
               #puts output1[index]
               result << PaymentVendor.find_by_Receipt_No(output[index])
               puts "Value of local variable is #{result}"

            }
        else
            @v_name=Vendor.where(:s_catagory => params[:payment][:s_catagory] ).pluck(:v_name)
        end
    end
end
  

check_type.js.erb:

<% if @v_name %>
$("#div_select").css("display", "block");
$("#name-option").html("<%= escape_javascript (render 'nameoption' ) %>");
$("#name-option").slideDown(350);
<% end %>
<% if @vendor_type  %>
console.log('hello')
$(".flash-message").html('<%= escape_javascript flash[:notice] %>');
$("#paymentdetail").css("display", "block");
$("#paymentoption").html("<%= escape_javascript (render 'paymentdetails' ) %>");
$("#paymentoption").slideDown(350);
<% end %>
  

_paymentdetails.html.erb:

<div><%= @vendor_type %></div>
        <table class="table table-bordered">
        <colgroup>
            <col class="col-md-1 col-sm-1">
            <col class="col-md-1 col-sm-1">
            <col class="col-md-3 col-sm-3">
            <col class="col-md-3 col-sm-3">
            <col class="col-md-4 col-sm-4">
        </colgroup>
        <thead>
            <tr>
                <th class="text-center"><input type="checkbox"></th>
                <th class="text-center">Sl. No</th>
                <th class="text-center">Date</th>
                <th class="text-center">Receipt No.</th>
                <th class="text-center">Amount</th>
            </tr>
        </thead>
        <tbody>
            <% result.each do |r| %>
            <tr>
                <th class="text-center"><input type="checkbox" id="checkbox1-1" name="checkbox1-1"></th>
                <td class="text-center"><%= r.id %></td>
                <td class="text-center"><%= r.c_date %></td>
                <td class="text-center"><%= r.Receipt_No %></td>
                <td class="text-center"><i class="fa fa-rupee"></i><%= r.v_amount %></td>
            </tr>
            <% end %>
        </tbody>
    </table>

此表将在payment.html.erb页面上呈现。我成功渲染空白表但我需要在此表中显示结果变量中的值。请帮我解决此错误。

2 个答案:

答案 0 :(得分:1)

如果要在视图中访问result,则应将其设为实例变量(@result)。

答案 1 :(得分:0)

resultcheck_type中本地定义,无法在视图中访问。 在视图和控制器中将其更改为@result