2013-09-26 45 views
0

我試圖寫公司模型的參數。但是我有錯誤未定義的方法`model_name'爲NilClass:Class在這一點上= simple_form_for @company,url:update_settings_company_path do | f |。我必須在哪裏設置@company?無法將參數寫入模型。 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

我修好了!在視圖中用'current_company'替換'@ 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

錯誤來自'update_settings_company_path',因此您需要在update_settings –

+0

中執行任何未定義的方法'model_name'for NilClass: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

它沒有幫助。不管未定義的方法'model_name'爲NilClass:Class –