2013-09-26 1 views
0

회사 모델에 매개 변수를 쓰려고합니다. 하지만 NilClass에 대한 정의되지 않은 메소드`model_name '에 오류가 있습니다.이 시점의 클래스 = [email protected], url : update_settings_company_path do | f |. @company를 어디에 설정해야합니까?Model에 params를 쓸 수 없습니다. Ruby on Rails

컨트롤러

def change_settings  
    @vacation_days = current_company.vacation_days 
    @illnes_days = current_company.illnes_days 
    end 

    def update_settings 
    if @company.update(company_params) 
     redirect_to account_company_path, notice: t('company.settings_changed') 
    else 
     render action: 'change_settings' 
    end 
    end 

private 
    def company_params 
    params.require(:company).permit(:vacation_days, :illnes_days) 
    end 

보기

.company_settings 
    = simple_form_for @company, url: update_settings_company_path do |f| 
    = f.error_notification 
    = f.input :vacation_days 
    = f.input :illnes_days 
    %br 
    = f.submit t('common.save'), class: 'btn' 
    = link_to t('common.back'), account_company_path, class: 'btn' 

루트

resource :company, only: :all do  
    get :change_settings 
    post :update_settings 
    patch :update_settings 
    end 

문제점은 무엇입니까? 도와주세요

+0

고쳐주었습니다. 보기에서'@ company'를'current_company'로 바꿨습니다. 그리고 메소드를 업데이트하기 위해'@company = current_company'를 추가했습니다. –

답변

0

두 동작 모두에서 @company 인스턴스 변수를 설정하지 마십시오. 이 같은 before_filter를 사용하여 수행 할 수 있습니다

before_filter :find_company 

def change_settings  
    @vacation_days = current_company.vacation_days 
    @illnes_days = current_company.illnes_days 
end 

def update_settings 
    if @company.update(company_params) 
    redirect_to account_company_path, notice: t('company.settings_changed') 
    else 
    render action: 'change_settings' 
    end 
end 

private 

    def company_params 
    params.require(:company).permit(:vacation_days, :illnes_days) 
    end 

    def find_company 
    @company = current_company 
    end 
+0

나는 데프 change_settings' change_settings 방법에 붙여 @company = current_company @vacation_days = current_company.vacation_days @illnes_days = current_company.illnes_days' 하지만 지금은 전무에 대해이 오류가 정의되지 않은 메서드 '갱신'얻을 : NilClass '데프을 update_settings if @ company.update (company_params)' –

+0

NilClass에 대해 정의되지 않은 메소드'model_name '이 무엇이든간에 –

+0

에서 update_settings에서 오류가 발생하기 때문에 오류가 발생합니다 : Class –

0

대신이 시도, 당신은 당신이 그것을 사용하기 전에 인스턴스 변수를 설정해야합니다. 기본적으로 인스턴스 변수는 nil로 설정됩니다.

def update_settings 
    @company = current_company 
    if @company.update(company_params) 
     redirect_to account_company_path, notice: t('company.settings_changed') 
    else 
     render action: 'change_settings' 
    end 
    end 
+0

도움이되지 않습니다. NilClass에 대해 정의되지 않은 메소드'model_name ': Class –