ruby on rails-Problem with the selection form helper

Posted by winter sun on Stack Overflow See other posts from Stack Overflow or by winter sun
Published on 2010-03-17T14:09:24Z Indexed on 2010/03/17 14:11 UTC
Read the original article Hit count: 312

Filed under:
|

Hello I have a form in witch users can add their working hours view them and edit them (All in one page). When adding working hours the user must select a project from a dropdown list. In case the action is adding a new hour record the dropdown field should remain empty (not selected) in case the action is edit the dropdown field should be selected with the appropriate value. In order to overcome this challenge I wrote the following code

<% if params[:id].blank?%>
     <select name="hour[project_id]" id="hour_project_id">
         <option value="nil">Select Project</option>
         <% @projects.each do|project|%>
             <option value="<%=project.id %>"><%=project.name%></option>
         <% end%>
    </select>
 <% else %>
   <%= select('hour','project_id', @projects.collect{|project|[project.name,project.id]},{:prompt => 'Select Project'})%>
 <% end %>

So in case of save action I did the dropdown list only with html, and in case of edit action I did it with the collect method. It works fine until I tried to code the errors. The problem is that when I use the error method: validates_presence_of :project_id it didn't recognize it in the html form of the dropdown list and don’t display the error message (its working only for the dropdown with the collect method).

I will deeply appreciate your instructions and help in this matter

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about select