Consider a blogging application whereby a user is trying to add a new
blog.
He/she has to select a category. Upon selecting a category from a drop
down box, a new sub categories drop down box will be created (hence an
AJAX call). Got this implemented and itâs ok.
Now, letâs assume that a given blog can have multiple categories (and
subcategories).
For example, a blog on a restaurant review can belong to the following
categories & subcategories:
âfoodâ -> âfine diningâ
âpeopleâ -> âlifestyleâ
âtravellingâ -> ârestaurantsâ
After selecting a set of category and sub category, there will be a
link entitled, âadd another category/subcategoryâ (I guess this can be
done with link_to_remote or something).
My question is, how do you code it such that upon clicking on the link
mentioned, another category drop down box will be displayed in the
form. Selecting a category value would then cause a sub categories
drop down box to appear in the same form allowing the user to select
another sub category. Again, the link entitled, âadd another category/
subcategoryâ will also be shown.
How do I go abouts implementing this? I know itâs a call to use AJAX
but I donât exactly know how. I have tried reading up some rails books
and cookbooks to no success.
Ultimately, youâre going to end up with arrays of category ids and
subcategory ids. Youâll represent these in your form with fields
named something like âcategory_id[]â and âsubcategory_id[]â. So your
ajax call will just need to return the html to insert a new properly-
named field, or perhaps just json representing the available options
and you create the new properly named field via DOM methods.
Render your select boxes with a partial showing the data.
ct9a wrote:
hi, there
Consider a blogging application whereby a user is trying to add a new
blog.
He/she has to select a category. Upon selecting a category from a drop
down box, a new sub categories drop down box will be created (hence an
AJAX call). Got this implemented and itâs ok.
Now, letâs assume that a given blog can have multiple categories (and
subcategories).
For example, a blog on a restaurant review can belong to the following
categories & subcategories:
âfoodâ -> âfine diningâ
âpeopleâ -> âlifestyleâ
âtravellingâ -> ârestaurantsâ
After selecting a set of category and sub category, there will be a
link entitled, âadd another category/subcategoryâ (I guess this can be
done with link_to_remote or something).
My question is, how do you code it such that upon clicking on the link
mentioned, another category drop down box will be displayed in the
form. Selecting a category value would then cause a sub categories
drop down box to appear in the same form allowing the user to select
another sub category. Again, the link entitled, âadd another category/
subcategoryâ will also be shown.
How do I go abouts implementing this? I know itâs a call to use AJAX
but I donât exactly know how. I have tried reading up some rails books
and cookbooks to no success.
this sounds like a plan but could you propose some links to read up
on?
Would be nice if some API doc was to mention on the syntax of having
arrays
in the form for select boxes.
In this case, some in depth documentation illustrating how
collection_select
can be used for arrays in the form. I suppose the answer to this
question would benefit many who are thinking of developing serious
apps with Rails.
Would love to do that but thereâs too many categories (and potentially
lots more to come). Hence,
the interface would be neater with select boxes representing
categories and further calls to subcategories.
link entitled, âadd another category/subcategoryâ
Have you considered using simple check boxes for categories with
an associated subcategory select (or radio button set)?
You could simplify the interface and eliminate some unnecessary
AJAX traffic.
1 - You load category
2 - In change event of the category you load sub category (Ajax)
3 - If user clicks add category link (inside addcategory div) , you do
an ajax updater and show the user a textbox
to enter his/her category
4- and when user clicks save, you reload category div and re-set
subcategory div (so user will be able to select his new category)
5 - subcategory is also same except you load only the sub categories
hope this helps,
cheers
sameera
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.