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 W. 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.


Michael W.

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> <p>Then in your select_packing_type method:</p> <p>def select_packing_type<br> render :update do |page|<br> case params[:packing_types][:type]<br> when “type 1”: page[‘packing_type_view’].replace_html :partial =><br> ‘type_one_partial’<br> […]<br> end<br> end<br> end</p> <p>This could be made DRYer in the case, but hopefully you get the idea.<br> When the user selects an option from the select drop down the value will<br> be passed to your controller and then the proper view will be rendered<br> from a partial and sent back to replace the current contents of the DIV<br> with the id of “packing_type_view”.</p> <p>David</p>

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs