On Aug 9, 2011, at 8:49 AM, Jarek Plonski wrote:

I'm creating my first Rails app and need an idea creating forms.

I have Meeting model with place attribute.

I want to have two fields for Meeting.place = one would be select with
places from other meetings or text_field if the place is being used
first time. User would be selecting radio_button of which field he has
used.

So is there any way to nest select and text_field within 2
radio_buttons?

I would appreciate any other solution ideas too.
Cheers


Here's how I usually solve this (requires Prototype, you'll have to translate if you're using jQuery):

<script type="text/javascript>
        if($('user_practice_id')){
$('user_practice_id').options[$('user_practice_id').options.length] = new Option('New...','New...');
                $('user_practice_id').observe('change',function(evt){
                        if($F(this) == 'New...'){
var text_input = new Element('input', {type:'text',size:'60',name:'practice_name',id:'practice_name'});
                                this.insert({before:text_input});
                                text_input.focus();
text_input['_bak'] = $ ('user_practice_id').hide().options.selectedIndex = 0;
                                text_input.observe('blur',function(evt){
                                        if($F(this) == '' && this._bak){
                                                this._bak.show();
                                                this.remove();
                                        }
                                });
                        }
                });
        }
</script>

In the controller, I test for the presence of the practice_name attribute, and if it's there, create a new Practice before assigning the practice to the User.

      if( params[:practice_name]  )
        practice = Practice.create({:name => params['practice_name']})
        params[:user][:practice_id] = practice.id
      end
      # regular save/update here

This gets you a "combo-box", where you can choose, or add new, in one interface "slot".

Walter


--
You received this message because you are subscribed to the Google Groups "Ruby on 
Rails: Talk" 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/rubyonrails-talk?hl=en.

Reply via email to