Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 以一种形式添加/编辑2个db表_Ruby On Rails - Fatal编程技术网

Ruby on rails 以一种形式添加/编辑2个db表

Ruby on rails 以一种形式添加/编辑2个db表,ruby-on-rails,Ruby On Rails,这里没有。我正在建立一个食谱网站,我有一个小问题。如果你有时间给我指出正确的方向,那就太好了 我有一张食谱表,上面有标题、描述、照片和准备时间 我还有一张配料表,上面有数量、调节剂,如杯子、毫升、配料、调节剂2切碎、切丁等 我无法在一页中添加/编辑配方及其成分,并将其填充到两个表中 我假设它非常简单,我只是忽略了一些基本的东西 我不需要冗长的回答,只需要指向正确的方向您需要查找嵌套表单: 简短的伪示例: # model attr_accessible :ingredients_attribute

这里没有。我正在建立一个食谱网站,我有一个小问题。如果你有时间给我指出正确的方向,那就太好了

我有一张食谱表,上面有标题、描述、照片和准备时间

我还有一张配料表,上面有数量、调节剂,如杯子、毫升、配料、调节剂2切碎、切丁等

我无法在一页中添加/编辑配方及其成分,并将其填充到两个表中

我假设它非常简单,我只是忽略了一些基本的东西


我不需要冗长的回答,只需要指向正确的方向

您需要查找嵌套表单:

简短的伪示例:

# model
attr_accessible :ingredients_attributes
accepts_nested_attributes_for :ingredients

# controller
def new
  @recipe = Recipe.new
  @recipe.ingredients.build
end

# new.html.erb
<% form_for(@recipe) do |f| %>
  <%= f.text_field(:name) %>
  <% fields_for(@recipie.ingredients) do |r|
    <%= r.text_field(:amount) %>
    <%= r.text_field(:name) %>
  <% end %>
  <%= submit_tag %>
<% end %>

您可以签出文档。

使用Rails 4,您需要将attr\u accessible移动到控制器def private中
class Recipe < ActiveRecord::Base
  has_many :ingredients
  accepts_nested_attributes_for :ingredients
end