accepts_nested_attributes_for无法正常工作

时间:2014-01-16 11:24:38

标签: ruby-on-rails ruby-on-rails-4 twitter-bootstrap-3 simple-form

我对rails很新,构建我的第一个应用程序。我正在运行rails 4 w / bootstrap 3.我正在尝试使用复杂的表单。我有两个模型:

class Employee < ActiveRecord::Base
    belongs_to :company
    belongs_to :user, :through => :company
    has_one :position
    accepts_nested_attributes_for :position
end

class Position < ActiveRecord::Base
    belongs_to :employees
    accepts_nested_attributes_for :employees
end

我有一个表单,用户可以在其中创建新职位(职位模型)并选择将应用该职位的员工(员工模型)。基本上,它是一个单独的表单,将字段添加到2个不同的数据库表(位置和员工)。

这是我的观点:

<%= simple_form_for(@position) do |f| %>
  <%= f.error_notification %>
  <div class="form-inputs">
    <%= f.input :job_title %>
    <%= f.input :job_description %>
  </div>
  <%= f.fields_for :Employee do |f| %>
        <%= f.input :employee_title, label: "Apply to:", collection: Employee.all, label_method: :first_name, as: :check_boxes %>
  <% end %>
  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %> 

以下是控制器:

class PositionsController < ApplicationController
  before_action :set_position, only: [:show, :edit, :update, :destroy]

  # GET /positions
  # GET /positions.json
  def index
    @positions = Position.all
  end

  # GET /positions/1
  # GET /positions/1.json
  def show
  end

  # GET /positions/new
  def new
    @position = Position.new
  end

  # GET /positions/1/edit
  def edit
  end

  # POST /positions
  # POST /positions.json
  def create
    @position = Position.new(position_params)

    respond_to do |format|
      if @position.save
        format.html { redirect_to @position, notice: 'position was successfully created.' }
        format.json { render action: 'show', status: :created, location: @position }
      else
        format.html { render action: 'new' }
        format.json { render json: @position.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /positions/1
  # PATCH/PUT /positions/1.json
  def update
    respond_to do |format|
      if @position.update(position_params)
        format.html { redirect_to @position, notice: 'position was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @position.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /positions/1
  # DELETE /positions/1.json
  def destroy
    @position.destroy
    respond_to do |format|
      format.html { redirect_to positions_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_position
      @position = Position.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def position_params
      params.require(:position).permit(:position_title, :position_description, :position_create_date)
    end
end

class EmployeesController < ApplicationController
    # encoding: UTF-8
  before_action :set_employee, only: [:show, :edit, :update, :destroy]

  # GET /employees
  # GET /employees.json
  def index
    @employees = Employee.all
  end

  # GET /employees/1
  # GET /employees/1.json
  def show
  end

  # GET /employees/new
  def new
    @employee = Employee.new
  end

  # GET /employees/1/edit
  def edit
  end

  # POST /employees
  # POST /employees.json
  def create
    @employee = Employee.new(employee_params)

    respond_to do |format|
      if @employee.save
        format.html { redirect_to @employee, notice: 'Employee was successfully created.' }
        format.json { render action: 'show', status: :created, location: @employee }
      else
        format.html { render action: 'new' }
        format.json { render json: @employee.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /employees/1
  # PATCH/PUT /employees/1.json
  def update
    respond_to do |format|
      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

  # DELETE /employees/1
  # DELETE /employees/1.json
  def destroy
    @employee.destroy
    respond_to do |format|
      format.html { redirect_to employees_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_employee
      @employee = Employee.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def employee_params
      params.require(:employee).permit(:first_name, :last_name, :employee_title)
    end
end

我面临的问题是我让表单完美呈现,但是当我提交它时,只会记录属于Position模型的字段。 :employee_title保持空白。有什么问题的建议吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

评论中有太多要点:

belongs_to :employees应该是单数的:belongs_to :employee

您的fields_for应该是(将 ff 与父表单区分开来):

<%= f.fields_for :Employee do |ff| %>
<%= ff.input :employee_title, label: "Apply to:", collection: Employee.all, 
                            label_method: :first_name, as: :check_boxes %>
<% end %>

如果它不起作用,也请提供控制器,我会更新我的答案。

修改

看到你的控制器之后,似乎最有可能出现unpermitted params

position_controller.rb中添加员工参数来定位参数

def position_params
  params.require(:position).permit(:position_title, :position_description, :position_create_date, employees_attributes: [:first_name, :last_name, :employee_title])
end