Angelo Cordova
2011-Oct-19 20:14 UTC
[Rails] Rails 3 - Make a “link_to” using selected value from collection select
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 -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Walter Lee Davis
2011-Oct-20 00:44 UTC
Re: [Rails] Rails 3 - Make a “link_to” using selected value from collection select
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, thanksThere''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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Angelo Cordova
2011-Oct-20 16:37 UTC
[Rails] Re: Rails 3 - Make a “link_to” using selected value from collection select
Great!!! Thanks for your help On 19 oct, 21:44, Walter Lee Davis <wa...-HQgmohHLjDZWk0Htik3J/w@public.gmane.org> wrote:> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > For more options, visit this group athttp://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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
yash shah
2013-Jul-27 01:06 UTC
Re: [Rails] Rails 3 - Make a “link_to” using selected value from collection select
Dear Sir, I have a similar issue. Here is my code: <h1>Add New Investment Opportunity</h1> <%= form_for Investopp.new do |f| %> <div class="field"> <%= f.label :state_id %><br/> <%= f.collection_select :state_id, Investopp.year_lookup(@state_ids), :label, :value, include_blank: true %> </div> <div class="field"> <%= f.label :city_id, "city" %><br /> <%= f.grouped_collection_select :city_id, Investopp.year_lookup(@state_ids), :cities, :value, :id,:name, include_blank: true %> </div> <% end %> <%= link_to ''Back'', investopps_path %> <%= link_to ''Search'', investopps_browsedisplay_path %> Here is the model(investopp): class Investopp < ActiveRecord::Base attr_accessible :Address, :Buildingname, :Desiredinvrole, :Details, :Prefferednoofinvestors, :Salesprice, :Weblisting, :photo, :user_id, :state_id, :city_id, :state, :city has_attached_file :photo, :styles => { :small => "200x200>" } belongs_to :user validates :Buildingname, presence: true validates :Address, presence: true validates :Desiredinvrole, presence: true validates :Weblisting, presence: true validates :Details, presence: true has_many :states has_many :cities def find_state(id) if !id || id==0 id=1 end @states= State.find(id) @states.name end def find_city(id) if !id || id==0 id=1 end @cities= City.find(id) @cities.name end def self.year_lookup(state_ids) #create an emptycollection to hold the LabelValue Objects years = [] state_ids.each do |yr| y = LabelValue.new() y.label = yr y.value = State.find_by_id(yr).name years.push(y) end years end def self.state_lookup(state_ids) years = [] state_ids.each do |yr| y = State.new() y= State.find_by_id(yr) years.push(y) end years end end class LabelValue # name the accessors. Label and Value attr_accessor :label, :value def cities cityids=[] state_cities=[] investopps=Investopp.find(:all) investopps.each do |i| puts i.city_id cityids <<i.city_id end cityids.uniq! states=State.find_by_id(label) cityids.each do |c| if states.cities.find_by_id(c) state_cities<< states.cities.find_by_id(c) end end state_cities end end Here is the controller: class InvestoppsController < ApplicationController # GET /investopps # GET /investopps.json def index @investopps = Investopp.where(:user_id => current_user.id) respond_to do |format| format.html # index.html.erb format.json { render json: @investopps } end end # GET /investopps/1 # GET /investopps/1.json def show @investopp = current_user.investopps.find(params[:id]) respond_to do |format| format.html # show.html.erb format.json { render json: @investopp } end end # GET /investopps/new # GET /investopps/new.json def new @investopp = Investopp.new respond_to do |format| format.html # new.html.erb format.json { render json: @investopp } end end # GET /investopps/1/edit def edit @investopp = Investopp.find(params[:id]) end # POST /investopps # POST /investopps.json def create #params[:investopp][:state_id]= "gopi" #params[:investopp][:city_id]= "33" @investopp = current_user.investopps.build(params[:investopp]) respond_to do |format| if @investopp.save format.html { redirect_to @investopp, notice: ''Investopp was successfully created.'' } format.json { render json: @investopp, status: :created, location: @investopp } else format.html { render action: "new" } format.json { render json: @investopp.errors, status: :unprocessable_entity } end end end # PUT /investopps/1 # PUT /investopps/1.json def update @investopp = Investopp.find(params[:id]) respond_to do |format| if @investopp.update_attributes(params[:investopp]) format.html { redirect_to @investopp, notice: ''Investopp was successfully updated.'' } format.json { head :no_content } else format.html { render action: "edit" } format.json { render json: @investopp.errors, status: :unprocessable_entity } end end end # DELETE /investopps/1 # DELETE /investopps/1.json def destroy @investopp = Investopp.find(params[:id]) @investopp.destroy respond_to do |format| format.html { redirect_to investopps_url } format.json { head :no_content } end end def lis @state_names=[] @state_ids=[] @city_ids= [] @city_names=[] @investopp = Investopp.find(:all) @investopp.each do |item| @state_names<< State.find_by_id(item.state_id).name @state_ids<< item.state_id @city_names<< City.find_by_id(item.city_id).name @city_ids << item.city_id end puts @state_ids.uniq!{|i| i} puts @city_ids.uniq!{|i| i} puts "gopi" respond_to do |format| format.html { render "investopps/lis", :locals => { :state_ids => @state_ids, :city_ids => @city_ids, :investopps => @investopp } } format.json { render json: @investopp } end end def redirect @state = State.find(:investopp [:id]) redirect_to @state end end My aim is to display the selected value from the drop down list on the other page. I tried applying the redirect method in the controller but I am not able to display the selected value. Please help. I am stucked up badly. Thanks in advance. On Wednesday, October 19, 2011 8:44:16 PM UTC-4, Walter Lee Davis wrote:> > 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 rubyonra...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<javascript:> > . > > To unsubscribe from this group, send email to > rubyonrails-ta...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org <javascript:>. > > 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 unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/fea3e59b-662f-4dc8-8188-28b07ac68e8c%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Colin Law
2013-Jul-27 15:10 UTC
Re: [Rails] Rails 3 - Make a “link_to” using selected value from collection select
On 27 July 2013 02:06, yash shah <yashshah1800-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Dear Sir, > I have a similar issue. Here is my code:Much TLDR. Also you replied to a thread that is two years old, so any comments there may well be out of date. Start a new thread, explain exactly what your problem is (one problem only in a posting otherwise the thread will get confusing) and post just the relevant section of code that you cannot get to work. Try to get the post down to around 20 lines total and you will have a good chance of someone reading it and trying to help. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLstYLtYhNjPfpiyO-hkN%2BzvNFsqEBScBajvcUm0hY09ew%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.