Rails - STI - 嵌套资源 - belongs_to列

时间:2014-01-06 18:28:32

标签: ruby-on-rails sti

不幸的是,我被困了,也许有人可以帮忙吗?谢谢!

我有3个类用户,经理和驱动程序。经理和驱动程序是子类 用户。 driver与manager有一个belongs_to关系:

class User < ActiveRecord::Base
  attr_accessible :email, :name
end

class Manager < User
has_many :drivers
end

class Driver < User
belongs_to :manager
end

我使用STI:

ActiveRecord::Schema.define(:version => 20140104122558) do

  create_table "users", :force => true do |t|
    t.string   "name"
    t.string   "email"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
    t.string   "type"
    t.integer  "manager_id"
  end

  add_index "users", ["manager_id"], :name => "index_users_on_manager_id"

end
添加了

manager_id以存储对管理器的引用,当在此列中创建新驱动程序时,应存储管理员标识,其中alas不起作用。 “manager_id”列保持为空。也许有人知道如何自动保存关联?

路线:

resources :managers do
    resources :drivers
  end

class ManagersController < ApplicationController

  def index
    @managers = Manager.all
  end

  def show
    @manager = Manager.find(params[:id])
  end

  def new
    @manager = Manager.new
  end

  def edit
     @manager = Manager.find(params[:id])
  end

  def create
    @manager = Manager.new(params[:manager])
if @manager.save
    # Handle a successful save.
    render 'new'
else
    render 'new'
end
  end

  def update
    @manager = Manager.find(params[:id])
  end

   def destroy
     @manager = Manager.find(params[:id])
     @manager.destroy
   end

 end



 class DriversController < ApplicationController

  def index
    @drivers = Driver.all
  end

  def show
    @driver = Driver.find(params[:id])
  end

  def new
    @driver     = Driver.new
@manager    = Manager.find_by_id(params[:manager_id])
  end

  def edit
    @driver = Driver.find(params[:id])
  end

  def create
    @driver = Driver.new(params[:driver])
@manager    = Manager.find_by_id(params[:manager_id])
if @driver.save
    # Handle a successful save.
    render 'new'
else
    render 'new'
end
  end

  def update
    @driver = Driver.find(params[:id])
  end

  def destroy
    @driver = Driver.find(params[:id])
    @driver.destroy
  end

end

在创建动作中不应该使用params [id]而不是params [manager_id]吗?

<h1>New driver</h1>

<%= form_for [@manager,@driver] do |f| %>
    <%= f.label :name %>
<%= f.text_field :name %><br />
<%= f.label :email %>
<%= f.text_field :email %><br />
<%= f.submit "Create new driver" %>
<% end %>

1 个答案:

答案 0 :(得分:0)

有一个要点,说明如何解决这个问题: Solution on github

控制器:

class ManagersController < ApplicationController

  def index
    @managers = Manager.all
  end

  def new
    @manager = Manager.new
  end

  def create
    @manager = Manager.new(params[:manager])
    if @manager.save
       redirect_to @manager
    else
       render 'new'
    end
  end

  def show
     @manager = Manager.find(params[:id])
  end

end


class DriversController < ApplicationController

   def index
    @manager = Manager.find(params[:manager_id])
    @drivers = @manager.drivers.all
   end

def new
    @manager = Manager.find(params[:manager_id])
    @driver = @manager.drivers.new
end

def create
    @manager = Manager.find(params[:manager_id])
    @driver = @manager.drivers.new(params[:driver])
    if @driver.save
        redirect_to [@manager, @driver]
    else
        render 'new'
    end
end

def show
    @manager = Manager.find(params[:manager_id])
    @driver = @manager.drivers.find(params[:id])
end

end