0

I trying write params to Company model. But I have error undefined method `model_name' for NilClass:Class in this point = simple_form_for @company, url: update_settings_company_path do |f|. Where I must set @company?

Controller

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

View

.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'

routes

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

What's wrong? Help me please

4

2 回答 2

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
于 2013-09-26T09:48:03.557 回答
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
于 2013-09-26T09:44:34.603 回答