On Oct 19, 2011, at 4:14 PM, Angelo Cordova wrote:

> 
> Hello everyone.
> 
> I have a form in my ruby 1.9.2 rails 3.0.9 app, and one of the fields
> is a "collection select" like this
> 
> <div class="field">
> <%= f.label :provider_id, "Provider" %>
> <%= collection_select( :purchase_document, :provider_id,
> Provider.all, :id, :name) %>
> </div>
> 
> The idea, is to be able to add a "link_to" using the selected value
> from the "collection select" i.e.:
> 
> <div class="field">
>    <%= f.label :provider_id, "Provider" %>
>    <%= collection_select( :purchase_document, :provider_id,
> Provider.all, :id, :name) %> <%= link_to "Show",
> provider_path(***selected option from collection select***)%>
> 
> But, I don't know how to get the selected value. Is there a rails way
> to do that?
> 
> Hope you can help me, thanks

There's two ways to do this, which one you choose depends on what stage you 
want the choice to be made in. If you want to submit a form to the controller, 
and then have the controller redirect based on the value of that select, you 
could do something like this (post to a "redirect" method):

def redirect
  @provider = Provider.find(:whatever_your_model_is[:provider_id]])
  redirect_to @provider
end

If you want this redirect to happen in the browser, without touching the 
server, then you would just observe the change event on the picker, and 
redirect based on that.

document.observe('dom:loaded', function(){
  $('whatever_your_model_is_provider_id').observe('change', function(evt){
    if(!!$F(this)){
      window.location.href = '/providers/' + $F(this);
    }
  });
});


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

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