Dynamically choosing views

I have a model (PackingTypes) that have all the different packing types
a warehouse person could want to pack with. I display this in the view
with a select. There is going to be a different view associated with
each packing type. I don't want individual links I would like a select
box. How would I go about "redirecting" to the appropriate packing
method associated with packing type selected once the form is
submitted?

Thanks,
-dustin

Dustin Withers wrote:

I have a model (PackingTypes) that have all the different packing types
a warehouse person could want to pack with. I display this in the view
with a select. There is going to be a different view associated with
each packing type. I don't want individual links I would like a select
box. How would I go about "redirecting" to the appropriate packing
method associated with packing type selected once the form is
submitted?

There's probably some way to do it writing JavaScript but a simpler way would probably be to use a case statement or some if/then/elses in the method that is called by the form submit and use redirect_to to redirect the request to the appropriate action.

One way to do this would be to have your select generate an AJAX request to the server and have the server return the correct view for that packing type using a different partial for each view type:

<%= select( :packing_types, :type,
                    @types.collect {|type| [type, type] },
                    {},
                     :onchange => remote_function( :with => "'packing_types[type]='+escape(value)",
                                                  :loading => "Element.show('loading-indicator6')",
                                                  :complete => evaluate_remote_response,
                                                  :url => { :action => :select_packing_type} ) ) %>
<div id='packing_type_view></div>

Then in your select_packing_type method:

def select_packing_type
    render :update do |page|
       case params[:packing_types][:type]
       when "type 1": page['packing_type_view'].replace_html :partial => 'type_one_partial'
       [...]
       end
    end
end

This could be made DRYer in the case, but hopefully you get the idea. When the user selects an option from the select drop down the value will be passed to your controller and then the proper view will be rendered from a partial and sent back to replace the current contents of the DIV with the id of "packing_type_view".

David

Michael Wang wrote: