2014-01-09 118 views
0

比方說我有一個型號Factory其中has_manyEmployees用form_for發送數據到控制器

在我看來,我有以下幾點:

%h3 
    = @factory.name 

%h3 Add new employee : 
= form_for :employee do |f| 
    .field 
    = f.label "Name :" 
    = f.text_field :name 
    .field 
    = f.label "Age :" 
    = f.text_field :age 
    .actions 
    = f.submit 'Save new employee' 

在我的控制,我想中檢索變量@factory這是在視圖的id。我怎樣才能訪問它?

謝謝。

回答

1

在呈現形式創建@employee實例的控制器,關聯到@factory

class EmployeesController < ApplicationController 
    def new 
    @factory = Factory.find params[:id] 
    @employee = @factory.employees.build 
    end 
end 

而且在視圖中使用實例@employee而不是:employee

= form_for @employee do |f| 
    ... 
+0

感謝您的快速答案=) – Pol0nium