I am trying to nest a form with two resources (Map and Location). I'm following the Nested Forms tutorial from Ryan Bates however I must be missing something.
Map Model:
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
Location Model:
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
View:
Controller(decorator):
Refinery::PagesController.class_eval do
before_filter :new
private
def new
@map = Map.new
3.times { @map.locations.build }
end
end
Schema:
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
Routes:
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
You are missing a
:locations_attributes
in your Map model - you have:questions_attributes
there instead.Also, check out this gem for refinerycms nested models - https://github.com/llamadigital/refinerycms-nested_models
Just replace "nested_models" with the name of the one you want to nest. It works much better with refinery than nested forms.