2014-07-04 31 views
0

我正在處理一個嵌套的屬性表單。Rails 4.0.0中的ActiveModel禁止錯誤

這是兩個模型.. Employee.rb

class Employee < ActiveRecord::Base 
    has_one :employee_info, :primary_key => :employeeID, foreign_key: :employeeID 
    accepts_nested_attributes_for :employee_info 
end 

EmployeeInfo.rb

class EmployeeInfo < ActiveRecord::Base 
    belongs_to :employee, primary_key: :employeeID, foreign_key: :employeeID 
    validates_uniqueness_of :employeeID 
end 

,我有我的_form.html.rb

<%= form_for @employee, html: {class: "form form-horizontal validate-form", novalidate: "novalidate"} do |f| %> 
        <% if @employee.errors.any? %> 
         <div id="error_explanation"> 
          <div class="alert alert-danger alert-dismissable"> 
           <a class="close" data-dismiss="alert" href="#">×</a> 
           <h2><%= pluralize(@employee.errors.count, "error") %> prohibited this shop from being saved:</h2> 
           <ul> 
            <% @employee.errors.full_messages.each do |msg| %> 
             <li><%= msg %></li> 
            <% end %> 
           </ul> 
          </div> 
         </div> 
        <% end %> 
        <div class='form-group'> 
         <%= f.label :employeeID, class: 'col-md-2 control-label' %> 
         <div class='col-md-5'> 
          <%= f.text_field :employeeID, {class: 'form-control'} %> 
         </div> 
        </div> 
        <div class='form-group'> 
         <%= f.label :employee_name, class: 'col-md-2 control-label' %> 
         <div class='col-md-5'> 
          <%= f.text_field :employee_name, class: 'form-control' %> 
         </div> 
        </div> 

        <%= f.fields_for :employee_info do |ff| %> 
         <div class='form-group'> 
          <%= ff.label :hired, class: 'col-md-2 control-label' %> 
          <div class='col-md-5'> 
           <%= ff.text_field :hire_date, class: 'form-control' %> 
          </div> 
         </div> 
         <div class='form-group'> 
          <%= ff.label :terminated, class: 'col-md-2 control-label' %> 
          <div class='col-md-5'> 
           <%= ff.text_field :term_date, class: 'form-control' %> 
          </div> 
         </div> 
        <% end %> 


        <div class='form-actions form-actions-padding-sm'> 
         <div class='row'> 
          <div class='col-md-10 col-md-offset-2'><i class='icon-save custom-icon'></i> 
           <% if params[:action] == "new" %> 
            <%= f.submit "Create", class: 'btn btn-primary custom-button' %> 
           <% else %> 
            <%= f.submit "Update", class: 'btn btn-primary custom-button' %> 
           <% end %> 
           <%= link_to 'Cancel', shops_path, class: 'btn' %> 
          </div> 
         </div> 
        </div> 
       <% end %> 

形式和控制器中的更新方法

def update 
    respond_to do |format| 
     p "------------------------------" 
     p employee_params 
     if @employee.update(employee_params) 
     format.html { redirect_to @employee, notice: 'Employee was successfully updated.' } 
     format.json { head :no_content } 
     else 
     format.html { render action: 'edit' } 
     format.json { render json: @employee.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

這裏是p employee_params在控制檯輸出是

{"employeeID"=>"103", "employee_name"=>"James Michule", "employee_info_attributes"=>{"hire_date"=>"1996-03-12 11:30:00 UTC", "term_date"=>"1996-03-12 11:30:00 UTC", "hourly_rate"=>"7.4", "address"=>"108 E. Jay", "phone_number1"=>"", "phone_number2"=>"", "zipcode"=>"65721", "state"=>"MO", "city"=>"Ozark", "id"=>"30"}} 

,當我嘗試更新我得到一個錯誤.. 錯誤:

ActiveModel::ForbiddenAttributesError (ActiveModel::ForbiddenAttributesError): 
    app/controllers/employees_controller.rb:56:in `block in update' 
    app/controllers/employees_controller.rb:53:in `update' 

有什麼不對?請幫助

+0

請問您可以發佈您的控制器嗎? – Emu

+1

你可以發佈你的控制器的'employee_params'方法嗎? – Pavan

回答

2

employees_controller

private 
    def employee_params 
    params.require(:employee).permit(:term_date, :hire_date) 
    end 

添加term_date & HIRE_DATEemployee_params方法類似上面的代碼。

希望它解決了這個問題。

P.S.並請閱讀主題在軌道上創建應用程序之前,「Rails的強大參數」> = 4

+0

好..這種情況下更新..但是在創建一個新的領域即時通訊使用相同的形式ñ第二種形式是不呈現任何想法? –

3

軌道4具有從strong_parameters功能

所以你可以使用

@employee.update_attributes(params[:employee], permit[:employee_attribute] 

,或者你可以做以下方式

@employee.update_attributes(params[:employee].permit(:employeeID)) 
+0

更新現在工作正常..但即時通訊使用相同的形式創建一個新的領域.. 但在視圖第二種形式(employee_info形式)不geting呈現..任何理由? –

+0

你可以看看development.log中的錯誤嗎?你得到了什麼錯誤 –

+0

謝謝你的努力。已經完全沒有錯誤。有些東西沒有被渲染到我的視圖:)現在已經存在 –