【问题标题】:How do I build a nested form in Refinerycms?如何在 Refinerycms 中构建嵌套表单?
【发布时间】: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


    【解决方案1】:

    您的地图模型中缺少:locations_attributes - 您有:questions_attributes 代替。

    另外,请查看此 gem 以了解炼油厂cms 嵌套模型 - https://github.com/llamadigital/refinerycms-nested_models

    只需将“nested_models”替换为您要嵌套的名称即可。与嵌套表单相比,它在炼油厂中的效果要好得多。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-03
      • 1970-01-01
      相关资源
      最近更新 更多