I have in my students table :
id | fname | gname | course | year |
----------------------------------------
1| user 1| user 1| IT | 5 |
----------------------------------------
2| user 2| user 2| CompE | 3 |
----------------------------------------
3| user 3| user 3| Nursing| 2 |
In my controller I am using *HASH to display the Student info*
def index
@students = Student.all
@studentlist = HASH.new
@students.each do |s|
@studentlist[s.id] = s
end
end
In my Index View I have this
<%=form_tag "/students/update_year" do%>
<table>
<tr>
<th>id</th>
<TH>Family Name</TH>
<TH>Year</TH>
</tr>
<%@studentlist.each_key do |key|%>
<tr>
<td><%=@studentlist[key].id%></td>
<td><%=@studentlist[key].fname%></td>
<td><%=collection_select(:student,:year,Student.select('distinct year') , :year , :year ,:selected=>@studentlist[key].year %></td>
</tr>
<%end%>
</table>
<%=submit_tag("Update")%>
- I want to change the year of the students and click the Update but my Problem is when I Update it all of the value in year column will be change and will have the same value.
This my controller update_year action :
def update_year
@students = Student.all
@studentlist = HASH.new
@students.each do |s|
@studentlist[s.id] = s
@studentlist.each_key do |key|
@student = Student.find(@studentlist[key].id)
@student.update_attributes(params[:student])
end
end
end
It will not update or the value will not be updated to I have set on my collection_select
Please help me ! Thank You.