2013-04-10 1 views
0

저는 ROR의 초보자입니다. 한 권의 책에서 코드를 테스트 할 때 문제가 발생했습니다. CaculatorController 번호에Ruby on Rails : NoMethodError

NoMethodError은 계산 ... 나는 단계에서 그것을 단계를 수행하지만 오류가 표시

undefined method `[]=' for nil:NilClass 

Rails.root :/홈/toth4321/계산기

app/controllers/caculator_controller.rb:4:in `set_charset' 

컨트롤러

class CaculatorController < ApplicationController 
    before_filter:set_charset 
    def set_charset 
    @headers['Content-Type'] = 'text/html; charset=GB2312' 
    end 

    def calculate 
    if request.post? 
     arg1 = convert_float(:arg1) 
     arg2 = convert_float(:arg2) 
     op = convert_operator(:operator) 
     @result = op.call(arg1, arg2) 
    end 
    end 

    private 
    def convert_float(name) 
    Float (params[name]) 
    end 

    def convert_operator(name) 
    case params[name] 
     when "+" then proc {|a,b| a+b} 
     when "-" then proc {|a,b| a-b} 
     when "*" then proc {|a,b| a*b} 
     when "/" then proc {|a,b| a/b} 
    end 
    end 
end 

자세히보기

<html> 
    <head> 
    <title>簡單的網頁計算器</title> 
    </head> 
    <body> 
    <%= form_tag(:action => :calculate) %> 
     <%= text_field_tag(:arg1, @params[:arg1], :size =>3) %> 
     <%= select_tag(:operator, options_for_select(%w{+ - * /})), @params[:operator])) %> 
     <%= text_field_tag(:arg2, @params[:arg2], :size =>3) %> 
     <%= submit_tag("送出") %> 
    <% end_form_tag %> 
    <b><%= @result %></b> 
    </body> 
</html> 

누군가 나를 도울 수 있습니까? 감사합니다.

답변

0

모르겠다. @headers은 무엇인지 모르지만, 내가 보는 한 어디에도 정의하지 않았다. 이 인스턴스 변수 아니다 이후

def set_charset 
    @headers = {'Content-Type' => 'text/html; charset=GB2312'} 
    end 
+0

는이 오류를 '/home/toth4321/calculator/app/controllers/calculator_controller.rb:4 표시 '] ='텍스트/html; charset = GB2312 ' ' –

+0

'='아니지만'=>' – Zippie

0

@headers에서 @을 제거, 오히려 response.headers 해시에 대한 위임 방법 : 시도

headers['Content-Type'] = 'text/html; charset=GB2312' 
0

다음 코드를 추가하여 실행 해보세요. keyword_end @headers = [ '콘텐츠 유형을 기대하고, 구문 오류, 예기치 않은'= ':

before_filter :set_charset 
def set_charset 
    @headers["Content-Type"] = "text/html; charset=GB2312" 
end