2015-02-11 57 views
0

我正在創建一個簡單的工作板,並希望顯示該格式的創建日期 - '發佈於:2015年2月11日'如何顯示發佈日期?

我想在兩個地方顯示它,在我的索引(index.html.erb)與崗位和個別帖子頁(show.html.erb)

我當前的代碼如下:

index.html.erb -

<p><%= link_to "Add a Position", new_job_path %></p> 

    <% @jobs.each do |job| %> 
     <div class="job"> 
      <%= link_to job.position, job %> 
      <br><br> 
      <ul class="entry_list"> 
       <li>Company: <%= job.company %></li> 
       <li>Salary: <%= job.salary %></li> 
       <li>Contract Type: <%= job.contract %></li> 
       <li>City: <%= job.city %></li> 
       <li>Expiry date: <%= job.expirydate %></li> 
       <li>Job Type: <%= job.jobtype %></li> 
      </ul> 
     </div> 
    <% end %> 

    <p><%= paginate @jobs %></p> 

show.html.erb -

<%= @job.position %> 
    <ul> 
     <li>Company: <%= @job.company %></li> 
     <li>Salary: <%= @job.salary %></li> 
     <li>Contract Type: <%= @job.contract %></li> 
     <li>City: <%= @job.city %></li> 
     <li>Expiry date: <%= @job.expirydate %></li> 
     <li>Job Type: <%= @job.jobtype %></li> 
     <li><%= @job.description %></li> 
     <li>How to apply: <%= @job.apply %></li> 
    </ul> 

job.rb -

class Job < ActiveRecord::Base 
    validates :position, presence: true 
    validates :company, presence: true 
    validates :salary, presence: true 
    validates :companywebsite, presence: true 
    validates :companytwitter, 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 
end 

jobs_controller.rb -

class JobsController < ApplicationController 
    def index 
    @jobs = Job.page(params[:page]).per(25) 
    end 

    def new 
    @job = Job.new 
    end 

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

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

回答

5

你需要使用的strftime了點。 「

<%= job.created_at.strftime("#{job.created_at.day.ordinalize} %B %Y") %> 

」張貼於「您需要靜態放置。

更多ordinalize

+0

這真棒,適用於索引頁面(index.html.erb),但是當我在show.html.erb中使用相同的代碼時,它返回'NameError in Jobs#show'錯誤,我該如何解決這個問題? – user2498890 2015-02-11 12:28:11

+0

在前面加一個'@' – 2015-02-11 13:19:12

+0

啊我明白了,謝謝你的幫助! – user2498890 2015-02-11 14:14:47

1

(這是一個除了japed's answer,但太長的評論)

因爲你需要這幾個地方,你可以添加你自己的時間格式:

# config/initializers/time_formats.rb 
Time::DATE_FORMATS[:short_ordinal] = ->(time) { time.strftime("#{time.day.ordinalize} %B %Y") } 

,並調用to_formatted_s或其別名to_s

job.created_at.to_s(:short_ordinal) 
#=> "11th February 2015" 
+1

另外,你也可以定義一個方法在模型中進行strftime轉換(或者如果使用它們,則是裝飾者/演示者)。 – 2015-02-11 13:29:23