Can I have a different workflow when using nested_form? - ruby-on-rails

I'm using the gem nested_form by Ryan Bates. I'm a bit confused about the workflow for the user.
I'm supposed to have a "f.link_to_add" call, that will dynamically add a nested structure for the association. That works as advertised, but I feel that it is cumbersome for the user to FIRST click "Add Book" (as in the commonly used Author/Books example) and THEN type in the new Book fields.
I would like to have an empty set of Book fields shown initially, and if the user fills them and clicks Save, the are added to the Author.
How would I use the nested_form gem to achieve this scenario?
Thx

I would suggest checking out build. For example, your new action for your author could have something like:
#author = Author.new
#author.books.build
Note that if an author could only have one book rather than multiple you'd do something like this instead:
#author = Author.new
#author.build_book

I think the short answer is that nested_form is not suitable for this particular workflow.
It's easier achieved with a separate form for the associated class where you in the controller lookup the parent class (Author) and in the Book form keep a hidden field (:author_id in this example).
This only works if you can accept to add Books one by one.
Nested_form is more suitable when you really want to show a long list of Books and let the user have the option of adding/removing multiple items before Saving.

Related

How do I create a rails simple_form with variable input fields

I have a PurchaseOrder Model that has_many Items. The form for PurchaseOrder needs variable input fields that will also save Items, where clicking the Add button will increase the fields in the page.
Here's what it will look like:
In order to achieve this:
How do I create a simple_form that will post the result of these multiple fields as an array to my existing PurchaseOrdercontroller where I can process and add these records?
Bonus: how would I handle this via Cucumber?
Edit: Why would you downvote a self contained question? Leave comments to explain when you downvote, please.
Use Cocoon as your nested forms for creating multiple fields.
You need to use simple_fields_for from simple_form gem. This allows you to work with attributes of associated models.
If you want to add new associated models via Add new button you need to create a new row. There is no code in pure simple_form that will help you with that. I found gem cocoon. It looks like it's what you're looking for. You can take a look how it's implemented there and make your own light solution.

In a single form, linking two instances of two models with habtm

I am using Rails 3.
I have a Product model and a Group model (a group has_many users, through membership).
I would like to build the new.html.erb form for the product model, and at the end of the form, I would like the user to be able to choose members from which group(s) can have access to the product he wants to add.
So, my goal is to list the groups to which the user belongs to, adding a checkbox for each of them. Then, create the associations between the product inserted and the different groups the user selected when the form is submitted, but I really do not understand how to achieve this, as all the documentation I have read use the BUILD or CREATE method that defines a new instance of group, instead of an existing one.
Is it possible with a nested form, and a HABTM relationship between product and group ? Or should I use a nested form with a has_many_through association using new model product_group_relationship ? Or should I use something else than a nested form ?
I'm quite new in Rails and a little bit lost here, so if some experienced guy could guide me a little bit, it would be very much appreciated!
The form_for helper comes with a nice package of extra methods like: fields_for wich makes you able to add nested attributes for has_many_through relations.
I suggest reading these:
http://apidock.com/rails/ActionView/Helpers/FormHelper/fields_for
And make sure you set your model validations accordingly

Create multiple objects (of similar type) with single form

Let's say I have a form for adding names and photos of dogs.
I would like to add up to 10 dogs on a single form.. and have any blank fields ignored...
How can I actually create such a form, and what would the controller action look like that would handle the form?
Thanks in advance
It seems like you'd want a sort of dynamic form, like Ryan Bates does on this RailsCast: http://railscasts.com/episodes/197-nested-model-form-part-2
You might want to check out the previous episode for rejecting the blank "dogs" too.

Friendship invite form with hobo

The hobo lifecycle tutorial shows how to implement a friendship logic in
the model and controller. However it does not really cover how to glue
the gui/views together. When I go to /friendships/invite - hobo
presents me with a form with a drop down menu. How do I add a form to
the user show-page with just one button (Invite) I guess that the the
user viewed should be in a hidden field?
I tried adding the form like this:
<extend tag="show-page" for="User">
<old-show-page merge>
<append-content-body:>
<invite-form for="Friendship" />
</append-content-body:>
</old-show-page>
</extend>
Hobo ignores the invite-form hmmm I must be missing something.
Best regards
Asbjørn Morell
try <invite-form:friendships.build/> assuming you have has_many :friendships on your user model. for is an attribute on the 'def' and 'extend' tags, it's not recognized by 'form'. To use a polymorphic tag, simply pass in an instance of the correct type in the context. This of course is easier if an instance already exists, but since you're using a creator action, just build one.
P.S. Questions like this get answered much quicker on the Hobo Users mailing list.

Using a select field with Rails nested object forms

Is it possible to use select fields with nested object forms feature of Rails 2.3?
Example:
Suppose you have a Article model, Category model, and a ArticleCategories join model. Article has_many Categories through ArticleCategories.
On our Edit Article form, you want to have an HTML select list of all the available categories. The user can select one or more Category names to assign to the Article (multiple select is enabled).
There are lots of ways to do this, but I'm wondering if there is a simple way to accomplish this using the nested objects feature. What would the form look like in your view?
Check out the nested form example from Github:
http://github.com/alloy/complex-form-examples
It's been a while since I looked at it, so I'm not sure if it covers exactly what you wanna do, but its a nice source for ideas / patterns.
Assuming you have defined the models and their relationships so you can do this:
#art = Article.find(article_id)
#art.categories # returns list of category objects this article is assigned to.
Then I usually use http://trendwork.kmf.de/175
You need to copy the JavaScript file into public/javascripts but after that you can just create the form element with something like:
swapselect(:article,#art,:categories,Category.find(:all).map { |cat| [cat.name, cat.id] })
(I would tend to wrap that in a helper to make the call even cleaner)
One small gotcha is that for very long lists it can run a little slow in IE6 because there's quite a lot of appendChild calls in the js which is notorioulsy slow in IE6
Update: Apologies. This doesn't really answer your original question, which was specifically about the Rails 2.3 feature. The swapselect option is version independent and doesn't make use of newer Rails functionality.

Resources