Topic: Rjs question?

I could use a little help and I'm not sure how to fix it.

I was doing some Combo dependent Select drop downs. I was able to achieve that part with an observe field.

Although everytime it calls the partial, it prints this along and it's "sandwhiching" the Select in the partial when called:

Element.update("subcategory_select", "\r\n  <Select DropDown>  \r\n\r\n ");

Any ideas what I'm doing wrong here?

Sam G.

Re: Rjs question?

Could you post the code you're using?

Railscasts - Free Ruby on Rails Screencasts

Re: Rjs question?

Opps fixed! I took out the :update in my observe_field.

Thanks for reading though.

Sam G.

Re: Rjs question?

Hey Ryan ;p

Thanks. I figured it out but I guess I posted the same time as you did but for posterity's sake and if people run into the same problem, I will post my code. I am not claiming it's the best solution but it works. smile

Here's the background:
REST with a Combo box for a Category/Subcategory dependent Select Dropdown using RJS

1. First, I had to customize my restful routes for it to recognize the additional update_subcategory action.

# routes.rb
map.resources :subcategorizations, :collection => { :update_subcategory => :post }, :member => { :update_subcategory => :post }

2. Second, we see the update_category method:
# subcategorizations_controller.rb =>action
# GET /onchange /subcategorization;update_select?parent_id=[:id] passed to @subcategorizations
  def update_subcategory
    @category = Category.find(params[:parent_id])
    @subcategorizations = Category.find(:all, :conditions => ['parent_id = ?', @category])
    respond_to do |format|

3. Third, I set an observe_field for the collection_select which calls on the update_subcategory action and renders a partial inside <div id="subcategory_select">
# /subcategorizations/new.rhtml
<% form_for(:subcategorization, :url => subcategorizations_path) do |f| %>
  <%= hidden_field :subcategorization, :directory_id %>
    <label><b>Main Category:</b></label>
       <%= collection_select :categorization, :subcategory_main,  Category.find(:all, :conditions => ['parent_id = 0', true]), :id, :name,
             :prompt => "Select Category" %>
       <%= observe_field :categorization_subcategory_main, :frequency => 0.1,
              :url => {:controller=>"subcategorizations",
              :with => "'parent_id=' + encodeURIComponent(value)"
     <div id="subcategory_select">
     <%# Will render the _subcategories.rhtml partial in this dom id element area %>

4. Here now is the RJS code that makes the action happen on the fly with onChange
# update_subcategory.rjs
page.replace_html :subcategory_select, :partial => 'subcategories'

5. Finally, this is the partial that gets rendered and is dependent on the first collection_select
# _subcategories.rhtml
<%= collection_select :subcategorization, :subcategory_id, @subcategorizations, :id, :name, :prompt => "Select Subcategory" %>

I sat on these codes for awhile trying to understand and figure everything. I am a newb so the custom restful routes was a little perplexing in the beginning. ;p

Thanks to the boards and all the helpful people in it. ;p

Keep rockin

Last edited by shajused (2007-07-08 09:15:01)

Sam G.

Re: Rjs question?

Thanks for this, shajused. It works very nicely. This is a good guide of how dependant dropdowns work with RJS.

Re: Rjs question?

No problem. smile

Sam G.