如何在activeadmin / formtastic中以深层嵌套的形式创建项目

时间:2021-10-30 15:57:48

I've been stuck for some time on how to create a deeply nested form using formtastic in ActiveAdmin. Here's a sample of my model structure:

我已经停留了一段时间,如何使用ActiveAdmin中的formtastic创建一个深度嵌套的表单。这是我的模型结构示例:

class Herb < ActiveRecord::Base
has_one :medicinal
attr_accessible :medicinal_attributes
accepts_nested_attributes_for :medicinal

A Herb has one Medicinal (use)

草药有一种药用(使用)

class Medicinal < ActiveRecord::Base
attr_accessible :recipe_attributes
belongs_to :herb
has_and_belongs_to_many :recipes
accepts_nested_attributes_for :recipes

A Medicinal (use) can have many Recipes

药用(使用)可以有很多食谱

class Recipe < ActiveRecord::Base
has_and_belongs_to_many :medicinals
has_many :recipe_ingredients
has_many :ingredients, :through => :recipe_ingredients
attr_accessible :recipe_ingredients_attributes
accepts_nested_attributes_for :recipe_ingredients

And a Recipe can have many Ingredients (through recipe_ingredients)

食谱可以有很多成分(通过recipe_ingredients)

class RecipeIngredient < ActiveRecord::Base
attr_accessible :ingredient_attributes
belongs_to :recipe
belongs_to :ingredient

Ingredient

成分

class Ingredient < ActiveRecord::Base
    attr_accessible :item
has_many :recipes, :through => :recipe_ingredients 

So here's my problem. I want the user, from the Herb Entry page in ActiveAdmin, to be able to create a recipe, to be able to have the Herb AUTOMATICALLY be entered as an ingredient, and if the user enters an ingredient that doesn't currently exist, to have it entered as a NEW ingredient (so other recipes can use it). I don't think I understand how to use the contoller in ActiveAdmin well enough to know where to begin... Here's what I have so far:

所以这是我的问题。我希望用户能够在ActiveAdmin的Herb Entry页面中创建一个配方,能够将Herb AUTOMATICALLY作为一种成分输入,如果用户输入了一个当前不存在的成分,让它作为新成分输入(所以其他食谱可以使用它)。我不认为我理解如何在ActiveAdmin中使用控制器,以便知道从哪里开始...这是我到目前为止所拥有的:

ActiveAdmin.register Herb do

controller do
 def new
  @herb = Herb.new
  @herb.build_medicinal
 end
 def edit
  @herb = Herb.find(params[:id])
  if @herb.medicinal.blank?
    @herb.build_medicinal  
  end
 end
end

  form do |f|
   f.inputs "Herb" do
   f.inputs :name => "Medicinal", :for => :medicinal do |med| 
    med.input :content,  :label => "Medicinal Uses", :required => true
     med.has_many :recipes do |r|
      r.inputs "Recipe" do
       r.has_many :recipe_ingredients do |i|
        i.inputs "Ingredients" do
          i.input :ingredient
        end
       end
      end
     end
    end 
   end
  f.actions
  end

I know this is long, but any advice you could give me would be greatly appreciated. I'm rather new to rails. Thanks!

我知道这很长,但你能给我的任何建议都将不胜感激。我对铁杆很新。谢谢!

1 个解决方案

#1


0  

If I were building the same site I might move the structure of the files around slightly to get the result you are looking for.

如果我正在构建相同的站点,我可能会稍微移动文件的结构以获得您正在寻找的结果。

app/admin/herb.rb

应用程序/管理/ herb.rb

ActiveAdmin.register Herb do
end

That should be all you need to create the page in active_admin. Then for your app/controller/herb_controller.rb

这应该是在active_admin中创建页面所需的全部内容。然后为您的app / controller / herb_controller.rb

class HerbController < ApplicationController
 def new 
  @herb = Herb.new
 end 
end   

I would now check to verify you can go to ActiveAdmin and create a new herb. I would further add to your app/controller/herb_controller.rb to check to see if a new ingredient needs to be added when you create your herb.

我现在检查以确认您可以转到ActiveAdmin并创建一个新草药。我会进一步添加到您的app / controller / herb_controller.rb,以检查在创建草药时是否需要添加新成分。

...
def create
 @herb = Herb.new
 if @herb.save
  @herb.create_ingredient_if_not_existing
  # perform any other actions, such as flash message or redirect 
 else
  # perform action if save not successful
 end
end

Now you want to create the method within your Herb model

现在,您要在Herb模型中创建方法

def create_if_not_existing
 unless Ingredient.where("name = ?", self.name) then
  new_ingredient = ingredient.build
  new_ingredient.name = self.name
  # add extra attributes here if necessary
  new_ingredient.save
 end
end

You can then make sure you aren't creating duplicates in either of your models with a line similar to this:

然后,您可以确保不使用与此类似的行在任一模型中创建重复项:

validates :name, :uniqueness => true

This is my first answer on Stack Overflow, so let me know if this was of any help to you! I struggled with the same stuff not too long ago and was lucky enough to have a couple of people help me out along the way.

这是我在Stack Overflow上的第一个答案,所以请告诉我这是否对你有任何帮助!不久前我和同样的东西挣扎过,很幸运,有几个人帮我一路走来。

#1


0  

If I were building the same site I might move the structure of the files around slightly to get the result you are looking for.

如果我正在构建相同的站点,我可能会稍微移动文件的结构以获得您正在寻找的结果。

app/admin/herb.rb

应用程序/管理/ herb.rb

ActiveAdmin.register Herb do
end

That should be all you need to create the page in active_admin. Then for your app/controller/herb_controller.rb

这应该是在active_admin中创建页面所需的全部内容。然后为您的app / controller / herb_controller.rb

class HerbController < ApplicationController
 def new 
  @herb = Herb.new
 end 
end   

I would now check to verify you can go to ActiveAdmin and create a new herb. I would further add to your app/controller/herb_controller.rb to check to see if a new ingredient needs to be added when you create your herb.

我现在检查以确认您可以转到ActiveAdmin并创建一个新草药。我会进一步添加到您的app / controller / herb_controller.rb,以检查在创建草药时是否需要添加新成分。

...
def create
 @herb = Herb.new
 if @herb.save
  @herb.create_ingredient_if_not_existing
  # perform any other actions, such as flash message or redirect 
 else
  # perform action if save not successful
 end
end

Now you want to create the method within your Herb model

现在,您要在Herb模型中创建方法

def create_if_not_existing
 unless Ingredient.where("name = ?", self.name) then
  new_ingredient = ingredient.build
  new_ingredient.name = self.name
  # add extra attributes here if necessary
  new_ingredient.save
 end
end

You can then make sure you aren't creating duplicates in either of your models with a line similar to this:

然后,您可以确保不使用与此类似的行在任一模型中创建重复项:

validates :name, :uniqueness => true

This is my first answer on Stack Overflow, so let me know if this was of any help to you! I struggled with the same stuff not too long ago and was lucky enough to have a couple of people help me out along the way.

这是我在Stack Overflow上的第一个答案,所以请告诉我这是否对你有任何帮助!不久前我和同样的东西挣扎过,很幸运,有几个人帮我一路走来。