我正在創建一個簡單的工作板,並希望顯示該格式的創建日期 - '發佈於: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
這真棒,適用於索引頁面(index.html.erb),但是當我在show.html.erb中使用相同的代碼時,它返回'NameError in Jobs#show'錯誤,我該如何解決這個問題? – user2498890 2015-02-11 12:28:11
在前面加一個'@' – 2015-02-11 13:19:12
啊我明白了,謝謝你的幫助! – user2498890 2015-02-11 14:14:47