You should not do this with eval , because then someone using your site can send any line of Ruby code for you, which would be a bad security hole for your site. You can check the string with regexp or whitelist, but that would be dirty.
I think you should rate 1.hour and 2.hours , etc. when rendering your form. Something like that:
<%= select_tag(:days_or_hours, options_for_select({ "1 hour" => 1.hour, "2 hours" => 2.hours })) %>
This generates HTML as follows:
<select name="days_or_hours"> <option value="3600">1 hour</option> <option value="7200">2 hours</option> </select>
Now the number of seconds will be transmitted when the form is submitted, and you do not need to worry about whether the user has selected hours or days. Your code will simply be:
time = Time.now + params[:days_or_hours].to_i
source share