S_Ahmed
(S Ahmed)
February 29, 2012, 3:44am
1
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?
Anthony3
(Anthony)
February 29, 2012, 5:29am
2
Set the html_options parameter in collection_select to { :selected => params[:friend_type] }
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
11155
(-- --)
March 1, 2012, 4:29am
4
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.
S_Ahmed
(S Ahmed)
March 1, 2012, 3:49pm
5
thanks I’ll try that tonight.
S_Ahmed
(S Ahmed)
March 3, 2012, 1:17am
6
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?
S_Ahmed
(S Ahmed)
March 3, 2012, 6:02pm
7
ok figured it out, I had to run seed on my test db.
thanks!