Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,12 @@ link_to_add_nested(form, :order_items, '#order-items', partial: 'my_partial')
link_to_add_nested(form, :order_items, '#order-items', partial_form_variable: :ff)
```

You can also pass more local variables to the partial by setting the partial_locals value.

```Ruby
link_to_add_nested(form, :order_items, '#order-items', partial_locals: { key: 'value' })
```

#### Tag

The HTML tag that will be generated. An `<a>` tag by default.
Expand Down
5 changes: 3 additions & 2 deletions lib/vanilla_nested/view_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@ module ViewHelpers
# @param link_classes [String] space separated classes for the link tag
# @param insert_method [:append, :prepend] tells javascript if the new fields should be appended or prepended to the container
# @param partial_form_variable [String, Symbol] name of the variable that represents the form builder inside the fields partial
# @param partial_locals [Hash] extra params that should be send to the partial
# @param tag [String] HTML tag to use for the html generated, defaults to and `a` tag
# @param link_content [Block] block of code for the link content
# @param tag_attributes [Hash<attribute, value>] hash with attribute,value pairs for the html tag
# @return [String] link tag
def link_to_add_nested(form, association, container_selector, link_text: nil, link_classes: '', insert_method: :append, partial: nil, partial_form_variable: :form, tag: 'a', tag_attributes: {}, &link_content)
def link_to_add_nested(form, association, container_selector, link_text: nil, link_classes: '', insert_method: :append, partial: nil, partial_form_variable: :form, partial_locals: {}, tag: 'a', tag_attributes: {}, &link_content)
association_class = form.object.class.reflections[association.to_s].klass
object = association_class.new

partial_name = partial || "#{association_class.name.underscore}_fields"

html = capture do
form.fields_for association, object, child_index: '_idx_placeholder_' do |ff|
render partial: partial_name, locals: { partial_form_variable => ff }
render partial: partial_name, locals: { partial_form_variable => ff }.merge(partial_locals)
end
end

Expand Down
5 changes: 4 additions & 1 deletion test/VanillaNestedTests/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ GEM
minitest (5.14.4)
msgpack (1.4.2)
nio4r (2.5.8)
nokogiri (1.12.5-x86_64-darwin)
racc (~> 1.4)
nokogiri (1.12.5-x86_64-linux)
racc (~> 1.4)
public_suffix (4.0.6)
Expand Down Expand Up @@ -212,6 +214,7 @@ GEM
zeitwerk (2.5.1)

PLATFORMS
x86_64-darwin-21
x86_64-linux

DEPENDENCIES
Expand Down Expand Up @@ -241,4 +244,4 @@ RUBY VERSION
ruby 2.6.6p146

BUNDLED WITH
2.2.29
2.3.22
6 changes: 3 additions & 3 deletions test/VanillaNestedTests/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# This file is the source Rails uses to define your schema when running `rails
# db:schema:load`. When creating a new database, `rails db:schema:load` tends to
# This file is the source Rails uses to define your schema when running `bin/rails
# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
# be faster and is potentially less error prone than running all of your
# migrations from scratch. Old migrations may fail to apply correctly if those
# migrations use external dependencies or application code.
Expand All @@ -13,7 +13,7 @@
ActiveRecord::Schema.define(version: 2021_02_20_015510) do

create_table "pets", force: :cascade do |t|
t.integer "user_id"
t.bigint "user_id"
t.string "name"
t.string "color"
t.index ["user_id"], name: "index_pets_on_user_id"
Expand Down