I have a form_for like: <%= form_for @user, .... |f| %> <%= f.collection_select :friend_type, FriendType.all, :id, :name %> Now in my new action, I set the friend_type from the querystring, so it already has a value of e.g. 3 (which is the id). How can I have this pre-selected when it renders? -- 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.
Anthony
2012-Feb-29 05:29 UTC
Re: how to pre-select a collection_select inside a form_for?
Set the html_options parameter in collection_select to { :selected => params[:friend_type] } On Tuesday, February 28, 2012 7:44:01 PM UTC-8, Gitted wrote:> > I have a form_for like: > > <%= form_for @user, .... |f| %> > > <%= f.collection_select :friend_type, FriendType.all, :id, :name %> > > > Now in my new action, I set the friend_type from the querystring, so it > already has a value of e.g. 3 (which is the id). > > How can I have this pre-selected when it renders? > > >On Tuesday, February 28, 2012 7:44:01 PM UTC-8, Gitted wrote:> > I have a form_for like: > > <%= form_for @user, .... |f| %> > > <%= f.collection_select :friend_type, FriendType.all, :id, :name %> > > > Now in my new action, I set the friend_type from the querystring, so it > already has a value of e.g. 3 (which is the id). > > How can I have this pre-selected when it renders? > > >On Tuesday, February 28, 2012 7:44:01 PM UTC-8, Gitted wrote:> > I have a form_for like: > > <%= form_for @user, .... |f| %> > > <%= f.collection_select :friend_type, FriendType.all, :id, :name %> > > > Now in my new action, I set the friend_type from the querystring, so it > already has a value of e.g. 3 (which is the id). > > How can I have this pre-selected when it renders? > > >On Tuesday, February 28, 2012 7:44:01 PM UTC-8, Gitted wrote:> > I have a form_for like: > > <%= form_for @user, .... |f| %> > > <%= f.collection_select :friend_type, FriendType.all, :id, :name %> > > > Now in my new action, I set the friend_type from the querystring, so it > already has a value of e.g. 3 (which is the id). > > How can I have this pre-selected when it renders? > > >On Tuesday, February 28, 2012 7:44:01 PM UTC-8, Gitted wrote:> > I have a form_for like: > > <%= form_for @user, .... |f| %> > > <%= f.collection_select :friend_type, FriendType.all, :id, :name %> > > > Now in my new action, I set the friend_type from the querystring, so it > already has a value of e.g. 3 (which is the id). > > How can I have this pre-selected when it renders? > > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/XeKXSvxhJyQJ. 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.
Dave Aronson
2012-Feb-29 20:30 UTC
Re: how to pre-select a collection_select inside a form_for?
http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/collection_select might help you on this. It seems to depend on what exact objects you''ve got set up. I''m not too clear on it myself! -Dave -- Dave Aronson: Available Cleared Ruby on Rails Freelancer (NoVa/DC/Remote) -- see www.DaveAronson.com, and blogs at www.Codosaur.us, www.Dare2XL.com, www.RecruitingRants.com -- 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.
Alpha Blue
2012-Mar-01 04:29 UTC
Re: how to pre-select a collection_select inside a form_for?
I''m curious what you have as far as relationships in your FriendType model? Does it belong_to :user? If so, <%= f.collection_select(:user_id, FriendType.all, :id, :name, options= {:prompt => false}, {:class => ''yourCustomClass''}) %> Hope that helps you out. -- Posted via http://www.ruby-forum.com/. -- 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.
S Ahmed
2012-Mar-01 15:49 UTC
Re: Re: how to pre-select a collection_select inside a form_for?
thanks I''ll try that tonight. On Wed, Feb 29, 2012 at 12:29 AM, Anthony <anthonyzacharakis-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote:> Set the html_options parameter in collection_select to { :selected => > params[:friend_type] } > > > On Tuesday, February 28, 2012 7:44:01 PM UTC-8, Gitted wrote: >> >> I have a form_for like: >> >> <%= form_for @user, .... |f| %> >> >> <%= f.collection_select :friend_type, FriendType.all, :id, :name %> >> >> >> Now in my new action, I set the friend_type from the querystring, so it >> already has a value of e.g. 3 (which is the id). >> >> How can I have this pre-selected when it renders? >> >> >> > On Tuesday, February 28, 2012 7:44:01 PM UTC-8, Gitted wrote: >> >> I have a form_for like: >> >> <%= form_for @user, .... |f| %> >> >> <%= f.collection_select :friend_type, FriendType.all, :id, :name %> >> >> >> Now in my new action, I set the friend_type from the querystring, so it >> already has a value of e.g. 3 (which is the id). >> >> How can I have this pre-selected when it renders? >> >> >> > On Tuesday, February 28, 2012 7:44:01 PM UTC-8, Gitted wrote: >> >> I have a form_for like: >> >> <%= form_for @user, .... |f| %> >> >> <%= f.collection_select :friend_type, FriendType.all, :id, :name %> >> >> >> Now in my new action, I set the friend_type from the querystring, so it >> already has a value of e.g. 3 (which is the id). >> >> How can I have this pre-selected when it renders? >> >> >> > On Tuesday, February 28, 2012 7:44:01 PM UTC-8, Gitted wrote: >> >> I have a form_for like: >> >> <%= form_for @user, .... |f| %> >> >> <%= f.collection_select :friend_type, FriendType.all, :id, :name %> >> >> >> Now in my new action, I set the friend_type from the querystring, so it >> already has a value of e.g. 3 (which is the id). >> >> How can I have this pre-selected when it renders? >> >> >> > On Tuesday, February 28, 2012 7:44:01 PM UTC-8, Gitted wrote: >> >> I have a form_for like: >> >> <%= form_for @user, .... |f| %> >> >> <%= f.collection_select :friend_type, FriendType.all, :id, :name %> >> >> >> Now in my new action, I set the friend_type from the querystring, so it >> already has a value of e.g. 3 (which is the id). >> >> How can I have this pre-selected when it renders? >> >> >> -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To view this discussion on the web visit > https://groups.google.com/d/msg/rubyonrails-talk/-/XeKXSvxhJyQJ. > > 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. >-- 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.
S Ahmed
2012-Mar-03 01:17 UTC
Re: Re: how to pre-select a collection_select inside a form_for?
Ok I added: page.html And my select is actually empty, it has no options! But when running rails server I can see the select and options (and the dropdown list in the browser). Why is it not rendering using capybara? My controller for new is: @account = Account.new My view page has: <% form_for .. %> <%= f.label :plan_type%> <%= f.collection_select :friend_type, FriendType.all, :id, :name, { :selected => params[:friend_type] } %> <%end%> My test looks like: describe "User pages" do subject { page } describe "new" do before { visit ''/user/new'' } describe "with valid information" do before do select ''XXXX'', :from => "user[friend_type]" fill_in ... ... end it "should create a user" do expect { click_button "Create"}.to change(User, :count).by(1) end end end end Is this a timing issue? On Wed, Feb 29, 2012 at 12:29 AM, Anthony <anthonyzacharakis-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote:> Set the html_options parameter in collection_select to { :selected => > params[:friend_type] } > > > On Tuesday, February 28, 2012 7:44:01 PM UTC-8, Gitted wrote: >> >> I have a form_for like: >> >> <%= form_for @user, .... |f| %> >> >> <%= f.collection_select :friend_type, FriendType.all, :id, :name %> >> >> >> Now in my new action, I set the friend_type from the querystring, so it >> already has a value of e.g. 3 (which is the id). >> >> How can I have this pre-selected when it renders? >> >> >> > On Tuesday, February 28, 2012 7:44:01 PM UTC-8, Gitted wrote: >> >> I have a form_for like: >> >> <%= form_for @user, .... |f| %> >> >> <%= f.collection_select :friend_type, FriendType.all, :id, :name %> >> >> >> Now in my new action, I set the friend_type from the querystring, so it >> already has a value of e.g. 3 (which is the id). >> >> How can I have this pre-selected when it renders? >> >> >> > On Tuesday, February 28, 2012 7:44:01 PM UTC-8, Gitted wrote: >> >> I have a form_for like: >> >> <%= form_for @user, .... |f| %> >> >> <%= f.collection_select :friend_type, FriendType.all, :id, :name %> >> >> >> Now in my new action, I set the friend_type from the querystring, so it >> already has a value of e.g. 3 (which is the id). >> >> How can I have this pre-selected when it renders? >> >> >> > On Tuesday, February 28, 2012 7:44:01 PM UTC-8, Gitted wrote: >> >> I have a form_for like: >> >> <%= form_for @user, .... |f| %> >> >> <%= f.collection_select :friend_type, FriendType.all, :id, :name %> >> >> >> Now in my new action, I set the friend_type from the querystring, so it >> already has a value of e.g. 3 (which is the id). >> >> How can I have this pre-selected when it renders? >> >> >> > On Tuesday, February 28, 2012 7:44:01 PM UTC-8, Gitted wrote: >> >> I have a form_for like: >> >> <%= form_for @user, .... |f| %> >> >> <%= f.collection_select :friend_type, FriendType.all, :id, :name %> >> >> >> Now in my new action, I set the friend_type from the querystring, so it >> already has a value of e.g. 3 (which is the id). >> >> How can I have this pre-selected when it renders? >> >> >> -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To view this discussion on the web visit > https://groups.google.com/d/msg/rubyonrails-talk/-/XeKXSvxhJyQJ. > > 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. >-- 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.
S Ahmed
2012-Mar-03 18:02 UTC
Re: Re: how to pre-select a collection_select inside a form_for?
ok figured it out, I had to run seed on my test db. thanks! On Fri, Mar 2, 2012 at 8:17 PM, S Ahmed <sahmed1020-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Ok I added: > > page.html > > And my select is actually empty, it has no options! But when running > rails server I can see the select and options (and the dropdown list in the > browser). > > Why is it not rendering using capybara? > > My controller for new is: > > @account = Account.new > > My view page has: > > <% form_for .. %> > > <%= f.label :plan_type%> > <%= f.collection_select :friend_type, FriendType.all, :id, :name, { > :selected => params[:friend_type] } %> > > <%end%> > > My test looks like: > > describe "User pages" do > > subject { page } > > describe "new" do > before { visit ''/user/new'' } > > describe "with valid information" do > before do > select ''XXXX'', :from => "user[friend_type]" > fill_in ... > ... > end > > it "should create a user" do > expect { click_button "Create"}.to change(User, :count).by(1) > end > > end > end > > end > > > Is this a timing issue? > > > On Wed, Feb 29, 2012 at 12:29 AM, Anthony <anthonyzacharakis-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote: > >> Set the html_options parameter in collection_select to { :selected => >> params[:friend_type] } >> >> >> On Tuesday, February 28, 2012 7:44:01 PM UTC-8, Gitted wrote: >>> >>> I have a form_for like: >>> >>> <%= form_for @user, .... |f| %> >>> >>> <%= f.collection_select :friend_type, FriendType.all, :id, :name %> >>> >>> >>> Now in my new action, I set the friend_type from the querystring, so it >>> already has a value of e.g. 3 (which is the id). >>> >>> How can I have this pre-selected when it renders? >>> >>> >>> >> On Tuesday, February 28, 2012 7:44:01 PM UTC-8, Gitted wrote: >>> >>> I have a form_for like: >>> >>> <%= form_for @user, .... |f| %> >>> >>> <%= f.collection_select :friend_type, FriendType.all, :id, :name %> >>> >>> >>> Now in my new action, I set the friend_type from the querystring, so it >>> already has a value of e.g. 3 (which is the id). >>> >>> How can I have this pre-selected when it renders? >>> >>> >>> >> On Tuesday, February 28, 2012 7:44:01 PM UTC-8, Gitted wrote: >>> >>> I have a form_for like: >>> >>> <%= form_for @user, .... |f| %> >>> >>> <%= f.collection_select :friend_type, FriendType.all, :id, :name %> >>> >>> >>> Now in my new action, I set the friend_type from the querystring, so it >>> already has a value of e.g. 3 (which is the id). >>> >>> How can I have this pre-selected when it renders? >>> >>> >>> >> On Tuesday, February 28, 2012 7:44:01 PM UTC-8, Gitted wrote: >>> >>> I have a form_for like: >>> >>> <%= form_for @user, .... |f| %> >>> >>> <%= f.collection_select :friend_type, FriendType.all, :id, :name %> >>> >>> >>> Now in my new action, I set the friend_type from the querystring, so it >>> already has a value of e.g. 3 (which is the id). >>> >>> How can I have this pre-selected when it renders? >>> >>> >>> >> On Tuesday, February 28, 2012 7:44:01 PM UTC-8, Gitted wrote: >>> >>> I have a form_for like: >>> >>> <%= form_for @user, .... |f| %> >>> >>> <%= f.collection_select :friend_type, FriendType.all, :id, :name %> >>> >>> >>> Now in my new action, I set the friend_type from the querystring, so it >>> already has a value of e.g. 3 (which is the id). >>> >>> How can I have this pre-selected when it renders? >>> >>> >>> -- >> You received this message because you are subscribed to the Google Groups >> "Ruby on Rails: Talk" group. >> To view this discussion on the web visit >> https://groups.google.com/d/msg/rubyonrails-talk/-/XeKXSvxhJyQJ. >> >> 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. >> > >-- 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.