我想使用活动管理员创建嵌套表单

时间:2017-04-22 15:27:39

标签: ruby-on-rails ruby ruby-on-rails-4

我想创建一个嵌套表单来使用活动管理员标记模型,但是它给了我一个错误,我不知道如何修复它。

这是错误日志。

Started GET "/admin/tags/1/edit" for ::1 at 2017-04-23 00:16:20 +0900
DEPRECATION WARNING: Active Admin: using `action_item` without a name is deprecated! Use `action_item(:edit)`. (called from action_item at /Users/Kohei/Desktop/blndd/x_tech/vendor/bundle/ruby/2.1.0/bundler/gems/activeadmin-6966a84c950b/lib/active_admin/dsl.rb:92)
DEPRECATION WARNING: Active Admin: using `action_item` without a name is deprecated! Use `action_item(:edit)`. (called from action_item at /Users/Kohei/Desktop/blndd/x_tech/vendor/bundle/ruby/2.1.0/bundler/gems/activeadmin-6966a84c950b/lib/active_admin/dsl.rb:92)
Processing by Admin::TagsController#edit as HTML
Parameters: {"id"=>"1"}
AdminUser Load (1.1ms)  SELECT  `admin_users`.* FROM `admin_users` WHERE `admin_users`.`id` = 1  ORDER BY `admin_users`.`id` ASC LIMIT 1
Tag Load (0.4ms)  SELECT  `tags`.* FROM `tags` WHERE `tags`.`id` = 1 LIMIT 1
CACHE (0.0ms)  SELECT  `tags`.* FROM `tags` WHERE `tags`.`id` = 1 LIMIT 1  [["id", "1"]]
Category Load (0.5ms)  SELECT `categories`.* FROM `categories` WHERE `categories`.`tag_id` = 1
Rendered vendor/bundle/ruby/2.1.0/bundler/gems/activeadmin-6966a84c950b/app/views/active_admin/resource/edit.html.arb (380.1ms)
Completed 500 Internal Server Error in 476ms (ActiveRecord: 39.7ms)

ActionView::Template::Error (undefined method `category' for #<Category:0x007fbf64900c60>):
1: insert_tag renderer_for(:edit)
app/admin/tag.rb:16:in `block (4 levels) in <top (required)>'
app/admin/tag.rb:15:in `block (3 levels) in <top (required)>'
app/admin/tag.rb:13:in `block (2 levels) in <top (required)>'



ActionView::Template::Error: undefined method `category' for #<Category:0x007fbf64900c60>

category.rb

这是我制作的类别模型的代码。

class Category < ActiveRecord::Base
    belongs_to :tag
end

tag.rb

class Tag < ActiveRecord::Base
    has_many :categories
    accepts_nested_attributes_for :categories, :allow_destroy => true
end
ActiveAdmin.register Tag do

permit_params :name, categories_attributes: [:category, :id, :name, :_destroy, :_edit, :_update]


form do |f|
  f.inputs do
    f.input :name
  end
 f.inputs "Categories" do
      #
      f.has_many :categories , allow_destroy: true, new_record: true  do |t|
        t.input :category
      end
    end
  f.actions
  end
end

1 个答案:

答案 0 :(得分:0)

category删除categories_attributes属性 - 您只需在此处输入类别属性。

此外,您可以删除f.inputs "Categories" do ... end,只需将has_many :categories添加到您的第一个inputs

permit_params :name, categories_attributes: [:id, :name, :_destroy, :_edit, :_update]

form do |f|
  f.inputs do
    f.input :name
    f.has_many :categories , allow_destroy: true, new_record: true  do |t|
      t.input :category
    end
  end

  f.actions
  end
end