Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 685883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:58:04+00:00 2026-05-14T01:58:04+00:00

I have a form in witch users can add their working hours view them

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T01:58:04+00:00Added an answer on May 14, 2026 at 1:58 am

    options_from_collection_for_select(collection, value_method, text_method, selected = nil) is your friend:

    <%= select('hour','project_id', options_from_collection_for_select(@projects, :id, :name, @hour.project_id),{:prompt => 'Select Project'})%>
    

    If there is an @hour object and it is new and therefore doesn’t have a project_id the value will be nil and nothing will be selected – alternatively the value of the @hour will be selected.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form where users can add input fields with jQuery. <input type="text"
I'm working on page that allows users to edit profile info. I want them
I have a form where users can enter data that ultimately would be used
I have this problem: My web application has a form where the users can
I have a dashboard with users details which they can edit. I would like
I have a form where users can subscribe and unsubcribe to my email list.
I have a form that allows the user to add information an their leisure.
I have this website (C#/ASP.NET) with a form where the user can register for
I have an application writted in ASP with a simple form. The users fill
I have a form that allows a user to update their profile information, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.