我是第一次使用Ruby on Rails创建一个简单的工作板,我需要弄清楚如果有人发布不合适的帖子或者由于某种原因它是不正确的,我将如何删除工作帖。我该如何删除那个帖子?
不确定此代码是否与此点相关,但如下所示:
jobs_controller.rb -
class JobsController < ApplicationController
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
end
index.html.erb -
<% @jobs.each do |job| %>
<div class="job">
<h2><%= link_to job.position, job %></h2>
<br><br>
<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 %>
<p><%= paginate @jobs %></p>
答案 0 :(得分:0)
对于添加管理员用户,最简单的方法是添加一个布尔字段is_admin
,可以将其设置为true和false,如@Marek Lipka所说。由于您只需要一个角色,这将是最好的方式。如果您需要复杂的角色,可以使用各种宝石,例如cancan
,authority
,rolify
。你可以检查其中任何一个。在视图中,您只需添加一个简单的if
案例来检查用户是否为管理员,然后只有delete
链接可见。要为此添加更多安全性,您可以在控制器中为before_filter
操作添加destroy
,您可以在其中检查删除帖子的用户是否为admin。如果没有,那么您可以重定向他或显示错误消息。希望这会有所帮助。