Skip to content

Allow setting defaults for new records on form.has_many - #5309

Closed
jspanjers wants to merge 2 commits into
activeadmin:masterfrom
jspanjers:has-many-new-record-defaults
Closed

jspanjers wants to merge 2 commits into
activeadmin:masterfrom
jspanjers:has-many-new-record-defaults

Conversation

@jspanjers

Copy link
Copy Markdown

Use example:

  form do |f|
    f.inputs do
      f.has_many :comments, new_record: { text: 'Leave comment, value: Comment.new(body: 'Active Admin rules!') } do |b|
        b.input :body
    end
  end

Use example:

```
  form do |f|
    f.inputs do
      f.has_many :comments, new_record: { text: 'Leave comment, value: Comment.new(body: 'Active Admin rules!') } do |b|
        b.input :body
    end
  end
```
@Fivell

Fivell commented Feb 8, 2018

Copy link
Copy Markdown
Member

@jspanjers interesting idea, don't you know if such behaviour can be emulated using :default option

f.has_many :comments, new_record: true  do |b|
        b.input :body, default: 'something'
    end

@faucct

faucct commented Mar 2, 2018 •

Copy link
Copy Markdown
Contributor

This could be improved to make it possible to supply several new records to show multiple Add ... links for an STI class, e.g.:

class Comment < ActiveRecord::Base
end
class UserComment < Comment
  belongs_to :user
end
class AnonymousComment < Comment
end

f.has_many :comments, new_record: [UserComment.new, AnonymousComment.new] do |fc|
  fc.input :body
  fc.input :user if f.object.is_a?(UserComment)
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants