Skip to content

Instantly share code, notes, and snippets.

@danielolivaresd
Last active September 9, 2016 19:41
Show Gist options
  • Save danielolivaresd/f708a1a6d671aac0c1ab8ca9469b5053 to your computer and use it in GitHub Desktop.
Save danielolivaresd/f708a1a6d671aac0c1ab8ca9469b5053 to your computer and use it in GitHub Desktop.
Rails i18n for accepts_nested_attributes_for (with has_many) using anchors
# Singular. This works for the following
# form_for @tree do |f|
# We can use an anchor (&tree) to have the values available
tree: &tree
name: Nombre
age: Edad
# Plural. This works for the following
# # forest.rb
# class Forest < ApplicationRecord
# has_many :trees
# accepts_nested_attributes_for :trees, reject_if: :all_blank
# end
#
# # tree.rb
# class Tree < ApplicationRecord
# belongs_to :forest
# end
#
# # form.html.haml
# form_for @forest do |f|
# f.fields_for :trees do |trees_form|
trees: *tree
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment