Skip to content

Instantly share code, notes, and snippets.

Created December 13, 2014 10:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/601ba9300fa462c6d4ab to your computer and use it in GitHub Desktop.
Save anonymous/601ba9300fa462c6d4ab to your computer and use it in GitHub Desktop.
class Bar < ActiveRecord::Base
belongs_to :foo
has_many :bazs
accepts_nested_attributes_for :bazs
end
class Baz < ActiveRecord::Base
belongs_to :bar
end
Started POST "/foos" for 127.0.0.1 at 2014-12-13 11:00:53 +0100
Processing by FoosController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"eLWiAFnILPySsw/b5h+BRtsk0UTbNJlsiTnllXvJuLuZGtxprb4lYTBmnvXsWqbmYnl3Rls0nW1tLz4+ydlu/w==", "foo"=>{"name"=>"foo", "bars_attributes"=>{"0"=>{"name"=>"bar1", "bazs_attributes"=>{"0"=>{"name"=>"baz1"}}}, "1"=>{"name"=>"bar2", "bazs_attributes"=>{"0"=>{"name"=>"baz2"}}}}}, "commit"=>"Create Foo"}
Completed 500 Internal Server Error in 1ms
ActionView::MissingTemplate (Missing template foos/create, application/create with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby]}. Searched in: ...
class Foo < ActiveRecord::Base
has_many :bars
accepts_nested_attributes_for :bars
end
class FoosController < ApplicationController
def new
@foo = Foo.new
@foo.bars.build
@foo.bars.build
@foo.bars.first.bazs.build
@foo.bars.last.bazs.build
end
private
def foo_params
params.require(:foo).permit(:name, bars_attributes: [:name, bazs_attributes: [:name]])
end
end
<%= form_for @foo do |f| %>
<p><%= f.text_field :name %></p>
<%= f.fields_for :bars do |bar| %>
<%= bar.text_field :name %>
<%= bar.fields_for :bazs do |baz| %>
<%= baz.text_field :name %>
<% end %>
<% end %>
<p><%= f.submit %></p>
<% end %>
Rails.application.routes.draw do
resource :foos
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment