我正在創建一個博客應用程序,用戶可以在其中發佈並只能查看他自己的帖子。只有一個管理員可以查看每個人發佈的帖子,並具有接受或拒絕用戶發帖的功能。例如,當用戶將分享他的職位,然後管理員將批准或拒絕該職位。如果他批准該職位將在那裏,如果管理員拒絕該職位將被刪除。我正在使用狀態機,但我有一些問題。兩個按鈕批准和拒絕,但他們不工作。
我只是想當我點擊批准按鈕,所以有一個列在我的帖子表默認值爲「待定」,所以它會改變批准或拒絕我點擊的任何按鈕....如果拒絕所以後應該點擊確認按鈕未定義的方法`許可證」爲 「state_event =批准」 時被刪除
:字符串Rails狀態機不能正常工作
[post.rb]
class Post < ActiveRecord::Base
require 'rubygems'
require 'state_machine'
has_many :documents
has_many :comments, dependent: :destroy
belongs_to :user
validates :title, presence: true, length: {in: 5..10}
validates :body, presence: true,length: {in: 5..200}
#:status is the column in posts table
state_machine :status, :initial => :pending do
#button name which is approve
event :approve do
transition :pending => :approve
end
event :decline do
transition :pending => :decline
end
end
def self.user_post(id)
role = User.find_role(id)
if role == 'admin'
Post.all
elsif role == 'user'
Post.where(user_id: id)
elsif role == 'developer'
Post.where(user_id: id)
end
end
def self.icon(extension)
case extension
when 'pdf'
EXTENSION[1]['pdf']
when 'png' || 'jpg' || 'jpeg'
EXTENSION[0]['png']
when 'doc' || 'odt'
EXTENSION[2]['doc']
end
end
####limit to upload files not more than ######
def self.upload(files,post_id)
files.each do |file|
@file_extension=file.content_type.split('/')[1]
doc = Document.new(document: file,post_id: post_id)
#save is a method which will save the content in the database
doc.save!
end
end
end
[post_controller]
class PostsController < ApplicationController
before_action :authenticate_user!
def index
@posts = Post.user_post(current_user).order('created_at DESC').paginate(:page => params[:page], :per_page => 5)
end
def new
@post = Post.new
end
def show
@post=find_params
end
def create
@post = Post.new(post_params)
@post.user = current_user
if @post.save
Post.upload(params[:post][:files],@post.id)
redirect_to @post
else
render 'new'
end
end
def edit
@post = find_params
end
def update
@post = find_params
if @post.update(post_params)
redirect_to @post
else
render 'edit'
end
end
def destroy
@post = find_params
@post.destroy
redirect_to posts_path
end
private
def post_params
p = params.require(:post)
if current_user.roles=='admin' # or however you check if the user is authorized
p.permit(:title, :body, :state_event)
else
p.permit(:title, :body)
end
end
def find_params
Post.find(params[:id])
end
end
[帖/ _form.html.erb]
<%= form_for @post,html: { multipart: true } do |f| %>
<% if @post.errors.any? %>
<div id="errors">
<h2><%= pluralize(@post.errors.count, "error") %> prevented this post from saving:</h2>
<ul>
<% @post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.label :title %><br>
<%= f.text_field :title %><br>
<br>
<%= f.label :body %><br>
<%= f.text_field :body %><br>
<br>
<!-- if you want to upload multiple files at a time -->
<%= f.label :files %><br>
<%= f.file_field :files,:multiple => true %><br>
<br>
<%= f.submit %>
<br>
<% end %>
<br>
[帖/ edit.html.erb]
<div id="page_wrapper">
<h1>Edit Post</h1>
<%= render 'form' %>
<br>
<%= form_for @post do %>
<%= button_to 'Approve', @post,
params: { post: { state_event: 'approve' } } ,
method: :patch
%>
<%= button_to 'Decline', @post,
params: { post: { state_event: 'decline' } } ,
method: :patch
%>
<% end %>
</div>
請解釋不起作用。 – Pavan
你無法理解的先生? –
什麼不起作用?你期望會發生什麼,實際發生了什麼?您需要自己實際執行一些調試步驟,例如檢查日誌。 – max