【发布时间】:2013-12-05 22:19:03
【问题描述】:
我正在尝试使用两个资源(地图和位置)嵌套一个表单。 我正在学习 Ryan Bates 的嵌套表单教程,但是我一定遗漏了一些东西。
地图模型:
module Refinery
module Maps
class Map < Refinery::Core::BaseModel
self.table_name = 'refinery_maps'
attr_accessible :name, :position, :questions_attributes
validates :name, :presence => true, :uniqueness => true
has_many :locations
accepts_nested_attributes_for :locations
end
end
end
位置模型:
module Refinery
module Maps
class Location < Refinery::Core::BaseModel
attr_accessible :latitude, :longitude, :address, :description, :title, :position
validates :address, :presence => true, :uniqueness => true
belongs_to :map
end
end
end
查看:
控制器(装饰器):
Refinery::PagesController.class_eval do
before_filter :new
private
def new
@map = Map.new
3.times { @map.locations.build }
end
end
架构:
create_table "refinery_maps_locations", :force => true do |t|
t.float "latitude"
t.float "longitude"
t.string "address"
t.string "description"
t.string "title"
t.integer "position"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "map_id"
end
create_table "refinery_maps", :force => true do |t|
t.string "name"
t.integer "position"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
路线:
Refinery::Core::Engine.routes.draw do
# Frontend routes
namespace :maps do
resources :maps, :path => '', :only => [:index, :show]
end
# Admin routes
namespace :maps, :path => '' do
namespace :admin, :path => Refinery::Core.backend_route do
resources :maps, :except => :show do
collection do
post :update_positions
end
end
end
end
# Frontend routes
namespace :maps do
resources :locations, :only => [:index, :show]
end
# Admin routes
namespace :maps, :path => '' do
namespace :admin, :path => "#{Refinery::Core.backend_route}/maps" do
resources :locations, :except => :show do
collection do
post :update_positions
end
end
end
end
end
【问题讨论】:
标签: refinerycms