Skip to content

Instantly share code, notes, and snippets.

@oren
Created March 26, 2010 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oren/345225 to your computer and use it in GitHub Desktop.
Save oren/345225 to your computer and use it in GitHub Desktop.
the plugin:
http://github.com/tamoyal/simple_time_select#readme
in the view: (u don't have to use date_select, but i needed it)
<%= f.date_select :published_at %>
<%= f.time_select :published_at,
{ :default => Time.now.change(:hour => Time.now.hour),
:simple_time_select => true,
:minute_interval => 30,
:time_separator => ""} %>
in the controller:
def update
params[:post] = fix_post_attrs(params[:post]) if params[:post]
@post = Post.find(params[:id])
respond_to do |format|
if @post.update_attributes(params[:post])
flash[:notice] = 'Post was successfully updated.'
format.html { redirect_to(@post) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @post.errors, :status => :unprocessable_entity }
end
end
end
private
def fix_post_attrs(post_attrs)
if full_time = post_attrs.delete("published_at(5i)")
post_attrs.merge!( {'published_at(4i)' => Time.parse(full_time).hour.to_s} )
post_attrs.merge!( {'published_at(5i)' => Time.parse(full_time).min.to_s} )
end
post_attrs
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment