Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.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 RubyonRails-在habtm关系中创建子表_Ruby On Rails_Ruby_Parent_Parent Child_Has And Belongs To Many - Fatal编程技术网

Ruby on rails RubyonRails-在habtm关系中创建子表

Ruby on rails RubyonRails-在habtm关系中创建子表,ruby-on-rails,ruby,parent,parent-child,has-and-belongs-to-many,Ruby On Rails,Ruby,Parent,Parent Child,Has And Belongs To Many,我有一个可以很好地创建的表列表。然后,我希望能够通过单击父表上的Edit(转到Edit.html.erb),然后单击New child来创建子表。父表和子表之间的关系是has_,并且_属于_many <%= button_to 'New Child', new_parent_child_path([@parent, @parent.children.build]), :method => :get %> 服务器日志提供了以下信息: Started GET "/parents/

我有一个可以很好地创建的表列表。然后,我希望能够通过单击父表上的Edit(转到Edit.html.erb),然后单击New child来创建子表。父表和子表之间的关系是has_,并且_属于_many

<%= button_to 'New Child', new_parent_child_path([@parent, @parent.children.build]), :method => :get %>
服务器日志提供了以下信息:

Started GET "/parents/1//children/new" for blah at blah
Processing by ChildrenController#new as HTML
  Parameters: {"parent_id"=>"1"}
  Rendered children/_form.html.erb (1.3ms)
  Rendered children/new.html.erb within layouts/application (1.9ms)
Completed 500 Internal Server Error in 4ms

ActionView::Template::Error (undefined method `children' for nil:NilClass):
    1: <%= form_for ([@parent, @parent.children.build]) do |f| %>
    2: 
    3: <div class = "field">
    4:   <%= f.label :Child_name %><br/>
  app/views/children      /_form.html.erb:1:in`_app_views_children__form_html_erb__2140243687_70034946643120'
  app/views/children/new.html.erb:3:in `_app_views_children_new_html_erb__655166082_70034947829100'

您需要在新操作中包括
@parent=parent.find(params[:parent_id])
,因为您正在new.html.erb模板中使用它。否则它将为零,
@parent.children.build
将不起作用。

您可以发布您的
新操作中的代码吗?已经添加了您请求的代码。您是一个绝对的明星,非常感谢!这件事我已经坚持了好几天了。我是个新手;我该如何说这是固定的,并为您提供站点使用的任何等效的rep/karma?欢迎!我将我的评论改为回答(见下文)。您可以单击复选标记以接受。有关更多信息,请参阅此链接:
Started GET "/parents/1//children/new" for blah at blah
Processing by ChildrenController#new as HTML
  Parameters: {"parent_id"=>"1"}
  Rendered children/_form.html.erb (1.3ms)
  Rendered children/new.html.erb within layouts/application (1.9ms)
Completed 500 Internal Server Error in 4ms

ActionView::Template::Error (undefined method `children' for nil:NilClass):
    1: <%= form_for ([@parent, @parent.children.build]) do |f| %>
    2: 
    3: <div class = "field">
    4:   <%= f.label :Child_name %><br/>
  app/views/children      /_form.html.erb:1:in`_app_views_children__form_html_erb__2140243687_70034946643120'
  app/views/children/new.html.erb:3:in `_app_views_children_new_html_erb__655166082_70034947829100'
<%= form_for ([@parent, @parent.children.build]) do |f| %>

<div class = "field">
  <%= f.label :Child_name %><br/>
  <%= f.text_field :ChildName %>
</div>

<div class = "field">
  <%= f.label :Child_email_address %><br/>
  <%= f.text_field :ChildEmail %>
</div>

<div class = "actions">
  <%= f.submit %>
</div>
<% end %>
  def new
    @child = Child.new
  end

  def create
    @parent = Parent.find(params [:parent_id])
    @scout = @parent.children.build(params[:child])
    redirect_to parent_path(@parent)
  end