我正在构建一个博客应用程序,我试图在这两个链接中有两个链接我将post_id发送到我的更新控制器,但它给了我错误。我想在我的更新控制器中收集post_id想要检查“状态”(状态基本上是我的帖子表中的列名,默认情况下我的状态列的状态为待处理状态。)当管理员点击批准时,列的状态应该更改为批准,当管理员点击时拒绝列的状态应该更改为拒绝也应删除用户帖子.Admin可以访问所有用户帖子,而用户可以访问他的唯一帖子
posts_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
puts "cccccccccc#{params[:commit]}"
Post.up(@post.id,params[:commit])
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
params.require(:post).permit(:title, :body)
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 %>
文章/ edit.html.erb
<div id="page_wrapper">
<h1>Edit Post</h1>
<%= render 'form' %>
<br>
</div>
迁移
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
t.string :title
t.text :body
t.integer :user_id
t.string :status
t.timestamps
end
end
end
文章/ show.html.erb
<div id="post_content">
<h1 class="title"><%= @post.title %></h1>
<p class="date">
Submitted <%= time_ago_in_words(@post.created_at) %> Ago
<% if user_signed_in? %>
<%= link_to 'Edit', edit_post_path(@post) %> |
<%= link_to 'Delete', post_path(@post), method: :delete, data: { confirm: 'Are you sure?' } %>
<%= link_to "approve",[:edit,@post] %>
<%= link_to "decline",[:edit,@post] %>
<% end %>
</p>
<p class="body"><%= @post.body %></p>
<div id="comments">
<h2><%= @post.comments.count %> Comments</h2>
<%= render @post.comments %>
<h3>Add a comment:</h3>
<%= render "comments/form" %>
</div>
</div>
post.rb
class Post < ActiveRecord::Base
has_many :documents
has_many :comments, dependent: :destroy
belongs_to :user
validates :title, presence: true, length: {in: 5..15}
validates :body, presence: true,length: {in: 5..200}
def self.up(id,params)
puts "aaaaaaaa#{id}"
puts "bbbbbbbbbbbbb#{params}"
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
的routes.rb
Rails.application.routes.draw do
root "posts#index"
devise_for :users
resources :posts do
resources :comments
end
resources :uploads
end
答案 0 :(得分:1)
您的代码存在许多问题。除了您面临的具体问题之外,我不会尝试解决所有这些问题。
首先,您需要为approve
和decline
操作生成路径..
resources :posts do
patch '/approve' => 'posts#approve', as: :approve #posts_approve_path(post) is the route helper for this
patch '/decline' => 'posts#decline', as: :decline #posts_decline_path(post) is the route helper for this
resources :comments
end
这将生成所需的路线。
现在在您要查看和拒绝的任何地方的视图中。
<%= link_to "approve",posts_approve_path(@post.id), method: :patch %>
<%= link_to "decline",posts_decline_path(@post.id), method: :patch %>
在您的控制器中,您需要修改find_params,因为生成的路线将:posts_id
作为:id
参数传递。
def find_params
id = params[:id] || params[:posts_id]
Post.find(id)
end
现在在您的控制器中添加2个新方法来实现该功能。
def approve
@post = find_params
@post.update_attribute(:status, 'approved') if @post.present?
redirect_to post_path(@post), notice: 'Post approved'
end
def decline
@post = find_params
@post.destroy if @post.present?
redirect_to post_path(@post), notice: 'Post deleted'
end