Skip to content

Instantly share code, notes, and snippets.

@biographie
Last active December 19, 2015 22:59
Show Gist options
  • Save biographie/6031300 to your computer and use it in GitHub Desktop.
Save biographie/6031300 to your computer and use it in GitHub Desktop.
class TasksController < ApplicationController
def index
end
def show
end
def new
@task = Task.new
@new_task.due_date_word = 'Tomorrow'
end
def create
@company = Company.find(params[:company_id])
@task = @company.tasks.create(params[:task])
if @task.save
redirect_to "/tasks"
else
render 'new'
end
end
def edit
end
end
<div class="container">
<div class="content pull-left">
<%= form_for(@task, :url => {:action => "create"}) do |f| %>
<%= f.label :description %>
<%= f.text_field :description, :class => "input-width bottom-border" %>
<%= f.label :task_category_id, "Choose a category" %>
<%= f.collection_select(:task_category_id, TaskCategory.all, :id, :task_category) %>
<%= f.label :due_date %>
<%= f.select :due_date_word, ['Today', 'Tomorrow', 'Next Week']%>
<%= f.submit "Add this Task", class: "btn" %>
<% end %>
</div>
</div>
# == Schema Information
#
# Table name: tasks
#
# id :integer not null, primary key
# title :text
# task_category_id :integer
# due_date :datetime
# description :text
# completed_at :datetime
# company_id :integer
# contact_id :integer
# user_id :integer
# created_at :datetime not null
# updated_at :datetime not null
#
class Task < ActiveRecord::Base
attr_accessible :company_id, :completed_at, :contact_id, :description, :due_date, :task_category_id, :title, :user_id, :due_date_word
belongs_to :company
belongs_to :task_category
belongs_to :contact
belongs_to :user
before_save :check_due_date_word
private
def check_due_date_word
case @due_date_word
when 'Today' then self.due_date = Time.now
when 'Tomorrow' then self.due_date = Time.now + 1.day
end
end
@jimmybaker
Copy link

class TasksController
  def new
    @task = Task.new
    @task.due_date_word = 'Tomorrow'
  end
end


class Task < ActiveRecord::Base
  attr_accessor :due_date_word
  before_save :check_due_date_word

  private
  def check_due_date_word
    case @due_date_word
    when 'Today' then self.due_date = Time.now
    when 'Tomorrow' then self.due_date = Time.now + 1.day
    end
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment