如何在Refinerycms中构建嵌套表单?

时间:2013-12-05 22:19:04

标签: refinerycms

我正在尝试使用两个资源(Map和Location)嵌套表单。 我正在关注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

1 个答案:

答案 0 :(得分:0)

您在地图模型中遗漏了:locations_attributes - 而是在那里:questions_attributes

另外,请查看此gem用于refinerycms嵌套模型 - https://github.com/llamadigital/refinerycms-nested_models

只需将“nested_models”替换为您要嵌套的名称。炼油厂比嵌套表格更好用。