无法在Rails 4

时间:2015-05-15 20:38:22

标签: ruby-on-rails-4 nested-forms nested-attributes formtastic

我总是遇到过has_many的麻烦:通过关联表上的关系再次陷入我出错的地方。我认为它可能在我的控制器中,因为我仍然在掌握它是如何工作的。

我有三种模式:

class Role < ActiveRecord::Base

    has_many :assignments, inverse_of: :role
    has_many :employees, :through => :assignments
    accepts_nested_attributes_for :assignments
end

class Employee < ActiveRecord::Base

  has_many :assignments, inverse_of: :employee
  has_many :roles, :through => :assignments
  accepts_nested_attributes_for :assignments

end

class Assignment < ActiveRecord::Base
    belongs_to :employee, inverse_of: :assignment
    belongs_to :role, inverse_of: :assignment
    accepts_nested_attributes_for :employee
    accepts_nested_attributes_for :role
end

我希望能够在创建或编辑员工时为员工设置预先创建的角色。我的新员工表格如下:

<%= semantic_form_for @employee do |f| %>
  <%= render 'shared/error_messages' %>
  <%= f.inputs do %>
    <%= f.input :first_name %>
    <%= f.input :last_name %>
    <%= f.input :email %>
    <%= f.input :password %>
    <%= f.input :password_confirmation %>
    <%= semantic_fields_for :roles do |role| %>
        <%= role.input :role, :as => :select, :collection => Role.all %>
        <%= role.semantic_fields_for :assignments do |assignment| %>
          <%= assignment.input :start_date, :as => :date_select %>
          <%= assignment.input :end_date, :as => :date_select %>
          <%= assignment.input :assignment_no %>
          <%= assignment.input :assignment_id %>
        <% end %>
      <% end %>
  <% end %>
  <%= f.actions do %>
    <%= f.action :submit, :as => :button %>
    <%= f.action :cancel, :as => :link %>
  <% end %>
<% end %>

最后我的员工控制员是:

class EmployeesController < ApplicationController
  before_action :set_employee, only: [:show, :edit, :update, :destroy]

  def index
    @employees = Employee.paginate(page: params[:page])
  end

  def show
  end

  def new
    @employee = Employee.new
    role = @employee.roles.build
  end

  def edit
     @employee = Employee.find(params[:id])
  end

  def create
    @employee = Employee.new(employee_params)
    if @employee.save
      @employee.send_activation_email
      flash[:info] = "Please check your email to activate your account."
      redirect_to root_url
    else
      render 'new'
    end
  end

  def update
    respond_to do |format|
      if @employee.update(employee_params)
        flash[:success] = "Profile updated"
        format.html { redirect_to @employee, notice: 'Employee was successfully updated.' }
        format.json { render :show, status: :ok, location: @employee }
      else
        format.html { render :edit }
        format.json { render json: @employee.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    Employee.find(params[:id]).destroy
    flash[:success] = "Employee deleted"
    respond_to do |format|
      format.html { redirect_to employees_url, notice: 'Employee was successfully destroyed.' }
      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(:avatar, :email, :first_name, :last_name, :password, :assignments_attributes => [:employee_id, :role_id, :roles_attributes => [:id, :employee_id, :PID]])
    end
end

我很欣赏这个主题总是在Stackoverflow上,但我似乎无法将任何问题转化为我所处的位置。任何帮助将不胜感激!

控制台输出如下:

Started POST "/employees" for 127.0.0.1 at 2015-05-16 18:05:34 +0200
source=rack-timeout id=d5933405c94d9c2e8bca3332564528ec timeout=60000ms service=25ms state=active
Processing by EmployeesController#create as HTML
  Parameters: {"utf8"=>"✓",     "authenticity_token"=>"TgHHELkt2zXUtS474WwGeHcmfKKw/broHxRdhlsF1P4JyGoa+03rchb6mfxbOSXKdrPgcJMeBCyIlCMHqPlQBA==", "employee"=>{"first_name"=>"John", "last_name"=>"Smith", "email"=>"john@smith.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "roles"=>{"role"=>"2", "assignments"=>{"start_date(1i)"=>"2012", "start_date(2i)"=>"7", "start_date(3i)"=>"5", "end_date(1i)"=>"2016", "end_date(2i)"=>"3", "end_date(3i)"=>"1", "assignment_no"=>"4", "assignment_id"=>"A3392822"}}, "button"=>""}
Unpermitted parameter: password_confirmation
   (0.2ms)  BEGIN
source=rack-timeout id=d5933405c94d9c2e8bca3332564528ec timeout=60000ms service=1113ms state=active
  SQL (70.3ms)  INSERT INTO "employees" ("email", "first_name", "last_name", "password_digest", "created_at", "updated_at", "activation_digest") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id"  [["email", "john@smith.com"], ["first_name", "John"], ["last_name", "Smith"], ["password_digest", "$2a$10$g3PNppheVZ8AFnnKuWg6secBdGev0NlCXjUx.RXsky03Xl9L3CubO"], ["created_at", "2015-05-16 16:05:35.422312"], ["updated_at", "2015-05-16 16:05:35.422312"], ["activation_digest", "$2a$10$L0yFuvmlJon7fWod6lHj..O7yDRaqwqTlTkJgD7Evqx.dA4pDTlBC"]]
   (133.0ms)  COMMIT

(我正在使用非常慢的电脑!)

0 个答案:

没有答案