Ruby on Rails - 如何在日期过后删除帖子?

时间:2015-02-13 14:06:14

标签: ruby-on-rails ruby

我目前有一个使用Ruby on Rails构建的作业列表站点,在添加作业页面上,其中一个输入是应用程序日期在/作业到期日期字段(<%= job.expirydate%>)。我需要添加代码,一旦该日期过去,将从视图中删除整个列表。目前,到期数据的输入字段只是文本输入,是否需要更改,如果是,如何更改?我是Rails的新手,所以请原谅我的知识。

index.html.erb -

<% @jobs.each do |job| %>
        <div class="job">
            <h2><%= link_to job.position, job %></h2>
            <ul class="entry_list">
                <li>Posted on: <%= job.created_at.strftime("#{job.created_at.day.ordinalize} %B %Y") %></li>
                <li>Company: <%= job.company %></li>
                <li>Salary: <%= job.salary %></li>
                <li>Contract Type: <%= job.contract %></li>
                <li>City: <%= job.city %></li>
                <li>Applications in by: <%= job.expirydate %></li>
                <li>Job Type: <%= job.jobtype %></li>
            </ul>
        </div>
    <% end %>

new.html.erb -

<%= simple_form_for @job, html: { multipart: true } do |form| %>
  <h2>Job Position:</h2>
    <%= form.input :position, input_html: { maxlength: 60 }, placeholder: "Job Position", label: false %>
    <%= form.input :company, input_html: { maxlength: 60 }, placeholder: "Company name", label: false %>
    <%= form.input :salary, input_html: { maxlength: 60 }, placeholder: "Salary", label: false %>
    <%= form.input :contract, input_html: { maxlength: 60 }, placeholder: "Contract Type", label: false, collection: ['Full time', 'Part time', 'Internship'], prompt: "Contract Type" %>
    <%= form.input :city, input_html: { maxlength: 60 }, placeholder: "City", label: false %>
    <%= form.input :expirydate, input_html: { maxlength: 60 }, placeholder: "Expiry date", label: false %>
    <%= form.input :jobtype, input_html: { maxlength: 60 }, placeholder: "Job Type", label: false, collection: ['Advertising', 'Art Direction', 'Artworker', 'Copywriting', 'Digital Design', 'Digital Developer', 'Editorial', 'Graphic Design', 'Miscellaneous', 'Motion Design/Animation', 'PR and Marketing', 'Project Management', 'Studio Management'], prompt: "Job Type" %>
    <%= form.input :description, input_html: { maxlength: 60 }, placeholder: "Full job description", label: false %>
    <%= form.input :apply, input_html: { maxlength: 60 }, placeholder: "How to apply", label: false %>
    <h2>Your Contact Details:</h2>
    <%= form.input :contactname, input_html: { maxlength: 60 }, placeholder: "Contact Name", label: false %>
    <%= form.input :contactemail, input_html: { maxlength: 60 }, placeholder: "Contact Email", label: false %>
    <%= form.input :contactphone, input_html: { maxlength: 60 }, placeholder: "Contact Telephone", label: false %>
    <%= form.input :human_sum, label: 'What is 1+1?' %>
    <%= form.button :submit %>
<% end %>

jobs_controller.rb -

class JobsController < ApplicationController

respond_to :html, :js

def index
   @jobs = Job.page(params[:page]).per(20).order(created_at: :desc)
end

def new
   @job = Job.new
end

def show
   @job = Job.find(params[:id])
end

def create
   @job = Job.new(params.require(:job).permit(:human_sum, :position, :company, :salary, :companywebsite, :contract, :city, :expirydate, :jobtype, :description, :apply, :contactname, :contactemail, :contactphone, ))
    if @job.save
      redirect_to root_path
    else
      render "new"
    end
end

end

job.rb -

class Job < ActiveRecord::Base
  validates :position, presence: true
  validates :company, presence: true
  validates :salary, presence: true
  validates :companywebsite, presence: true
  validates :contract, presence: true
  validates :city, presence: true
  validates :expirydate, presence: true
  validates :jobtype, presence: true
  validates :description, presence: true
  validates :apply, presence: true
  validates :contactname, presence: true
  validates :contactemail, presence: true
  validates :contactphone, presence: true

  attr_accessor :human_sum 
  validate :not_a_bot

  private

  def not_a_bot
    if human_sum.to_i != 2 
      errors.add(:human_sum, 'Get out, you bot!') 
    end
  end

  scope :by_contract, -> (contracts) { where(:contract => (contracts|| Job.uniq.pluck(:contract)) ) }
  scope :by_jobtype, -> (jobtypes) { where(:jobtype => (jobtypes|| Job.uniq.pluck(:jobtype)) ) }

end

4 个答案:

答案 0 :(得分:1)

您可以将控制器中的索引操作更改为:

Model.find(:all, :conditions => [job.expirydate < Datetime.now])

如果要删除旧记录,可以为任务创建文件:

# lib/tasks/delete_old_records.rake
namespace :delete do
  desc 'Delete records older than 60 days'
  task :old_records => :environment do
    Model.where('created_at > ?', 60.days.ago).each do |model|
      model.destroy
    end

    # or Model.delete_all('created_at > ?', 60.days.ago) if you don't need callbacks
  end
end

使用以下命令运行:

RAILS_ENV=production rake delete:old_records

安排它与cron一起运行(在本例中每天早上8点):

0 8 * * * /bin/bash -l -c 'cd /my/project/releases/current && RAILS_ENV=production rake delete:old_records 2>&1'

您还可以使用[when] [1] gem在部署时创建和管理crontab:

every 1.day, :at => '8:00 am' do
  rake "delete:old_records"
end

答案 1 :(得分:0)

我会做一个rake任务,删除所有旧帖子,然后定期从cron或其他调度程序运行它,例如每天凌晨2点。

编辑 - 虽然,再看一下你的问题,但我不认为你真的想要删除它们。我想你只是想不在索引页面上显示它们。混淆使用单词&#34;删除&#34;那里:))

答案 2 :(得分:0)

要将显示限制为活动记录,请向模型添加范围:

scope :current, -> { expirydate >= Date.current }

并将其添加到您的控制器:

@jobs = Job.current.page(params[:page]).per(20).order(created_at: :desc)

答案 3 :(得分:0)

2件事:
1)仅列出未过期的工作

def index
   @jobs = Job.where('expirydate >= ?', Date.today).page(params[:page]).per(20).order(created_at: :desc)
end

2)使用交互式日期选择器增强expirydate的文本输入。 在日期选择器中构建的导轨并不是很好,因为他们不使用javascript。根据您使用的javascript库,有很多解决方案。例如,对于jquery,请参阅http://jqueryui.com/datepicker/