Yes you can.
Don't touch the new method. For those cases you have to override de do_new
method
def do_new
@record = Foo.new
apply_constraints_to_record(@record)
[:baz, :lol, :omg].each do |desc|
@foo.bars.build(:desc => desc)
end
if nested?
create_association_with_parent(@record)
register_constraints_with_action_columns(nested.constrained_fields)
end
@record
end
something like that. Check lib/active_scaffold/actions/create.rb for more
options.
For the form, you don't need to do anything, just add :bars to create
columns and AS would do the job for you.
config.create.actions.add :bars
There are options for that also.
for 2) you can use an "action_link"
and 3) didn't get it. sorry :(
enjoy,
On Wed, Sep 14, 2011 at 5:21 AM, patrick99e99 <[email protected]>wrote:
> 1) Let's say I have a model Foo that has_many :bars.
>
> When viewing the active_scaffold Foo list, is there a way to make the
> "+ new foo" action button prepare a new foo with pre-built associated
> records?
>
> In other words, in a normal controller I could do something like this:
>
> def new
> @foo = Foo.new
> [:baz, :lol, :omg].each do |desc|
> @foo.bars.build(:desc => desc)
> end
> end
>
> So this way, in a regular form, I could do something like:
>
> form_for @foo do |f|
> f.fields_for :bars do |bar|
> bar.label bar.object.desc
> bar.text_field bar.object.desc
> end
> end
>
> And this would generate a form with inputs like:
> baz: _______
> lol: ________
> omg: _______
>
> ...
>
> So basically, I am looking for a way to dynamically build form inputs
> for my view, and am wondering how this might be achieved with active
> scaffold.
>
> 2) If my previous question can't be answered, then is there simply an
> easy way that I can make my "+ new foo" button on the active_scaffold
> list page render a custom "new" action (so I can have it do what I
> described in #1) ?
>
> I'm either looking for a way that I could make active_scaffold render
> a custom new action inline with the list view, or actually just do a
> page refresh and bring up the new action.. Either way is fine, but
> ideally if I could accomplish what I am looking for with #1, then that
> would be super ideal.
>
> ...
>
> Actually I have a #3 question too.. Supposing that #1 is possible, is
> there a way to tell active scaffold to include a custom variable when
> triggering the new action?
>
> For example, lets say I want to have a select on the page next to my
> "+ new foo" button..
>
> This select would have options: "lol" and "omg"
>
> So, by clicking "+ new foo", it would do a query string ?item=lol or ?
> item=omg, and this way I could have the controller action do
> something like:
>
> def new
> @foo = Foo.new
>
> descs = case params[:item]
> when "lol"
> [:lol, :lotcats, :rotflmao]
> when "omg"
> [:omg, :whoa]
> end
>
> descs.each do |desc|
> @foo.bars.build(:desc => desc)
> end
> end
>
> ... Hopefully you can see where I am going with this... Is this at
> all possible?
>
> -patrick
>
> --
> You received this message because you are subscribed to the Google Groups
> "ActiveScaffold : Ruby on Rails plugin" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/activescaffold?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups
"ActiveScaffold : Ruby on Rails plugin" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/activescaffold?hl=en.