I am having a strange issue with a collection_select I am using in an edit profile view in my rails application. The database IS being updated with the correct value, however the default value is not being selected for the select box when the user goes 开发者_StackOverflowto edit their profile. I can not get a :include_blank => true or a :prompt => true to work either.
Here is the code
Edit View:
<%= render :partial => 'player_form' %>
_player_form Partial:
<% form_for @user do |f| %>
<p>
<%= f.label :course_id %><br / >
<%= f.collection_select(:course_id, Course.all, :id, :name, {:prompt => "No Home Course"}) %>
</p>
<% end %>
Generated HTML:
<p>
<label for="user_course_id">Course</label><br / >
<select id="user_course_id" name="user[course_id]">
<option value="1">Bedford Springs</option>
<option value="2">Down River</option>
<option value="3">King Valley </option>
<option value="4">Test</option></select>
</p>
Because I am using cancan for authorization I have an empty edit action in my controller since cancan automatically initializes
@user = User.find(params[:id])
I have tried adding that line to edit and it doesn't affect anything. Also the very strange thing about this issue is that all of the other fields in the edit form partial are populated with correct current values from the database. In addition, the Users table does have the course_id column which is the foreign key for the Courses table and I have checked the association in the console and it does return the correct course_id. The associations are setup properly because I have Therefore, I believe I have a problem with the syntax of my collection_select but I am not sure what it is.
I appreciate any help.
Thanks.
I had course_id set as a varchar instead of an int in the database for one reason or another. Changing it too an int fixed the problem.
精彩评论