2015-11-28 28 views
2

我想在我的应用程序中删除帖子。它在localhost中工作正常,但是当我推送到heroku时,它不起作用。我收到一条错误消息:“出错了,请检查日志”。这里是我的代码:Heroku错误删除生产中的帖子:Rails

posts_controller.rb

class PostsController < ApplicationController 
    before_action :set_post, only: [:show, :edit, :update, :destroy] 
    before_action :authenticate_user! , except: [:index,:show,:search] 
    before_filter :check_user, only: [:edit,:update,:destroy] 




    # GET /posts 
    # GET /posts.json 

    def search 
    if params[:search].present? 
    @posts = Post.search(params[:search]) 
    else 
    @posts = Post.all 
    end 
    end 

    def index 
    if params[:tag] 
     @posts = Post.tagged_with(params[:tag]) 
    else 
     @posts = Post.all 
    end 
    end 

    # GET /posts/1 
    # GET /posts/1.json 
    def show 
    @reviews = Review.where(post_id: @post.id) 

    end 

    # GET /posts/new 
    def new 
    @post = Post.new 
    end 


    # GET /posts/1/edit 
    def edit 
    @post = Post.find(params[:id]) 
    end 

    # POST /posts 
    # POST /posts.json 
    def create 
    @post = Post.new(post_params) 
    @post.user_id = current_user.id 

    respond_to do |format| 
     if @post.save 
     format.html { redirect_to @post, notice: 'Post was successfully created.' } 
     format.json { render :show, status: :created, location: @post } 
     else 
     format.html { render :new } 
     format.json { render json: @post.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PATCH/PUT /posts/1 
    # PATCH/PUT /posts/1.json 
    def update 
    respond_to do |format| 
     if @post.update(post_params) 
     format.html { redirect_to root_url, notice: 'Post was successfully updated.' } 
     format.json { render :show, status: :ok, location: @post } 
     else 
     format.html { render :edit } 
     format.json { render json: @post.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /posts/1 
    # DELETE /posts/1.json 
    def destroy 
    @post.destroy 
    respond_to do |format| 
     format.html { redirect_to root_path, notice: 'Post was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_post 
     @post = Post.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def post_params 
     params.require(:post).permit(:title, :description,:image,:all_tags) 
    end 

    def check_user 
     if current_user.id != @post.user_id 
     redirect_to root_path , alert: "Sorry this Post belongs to someone else" 
    end 
    end 

end 

(日志)

enter image description here

enter image description here

图/职位/ index.html.erb

<h3>Posts</h3> 
<table class="table"> 
    <thead> 
    <tr> 

     <th colspan="3"></th> 
    </tr> 
    </thead> 
    <tbody> 
    <% @posts.each do |post| %> 
     <tr> 
     <td><h4><%=link_to post.title , post%></h4></td> 
     <td><%=raw tag_links(post.all_tags)%></td> 
      <td><%= link_to 'Edit', edit_post_path(post) %></td> 
      <td><%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %></td> 
     </tr> 
    <%end%> 
    </tbody> 
</table> 

型号/ post.rb

class Post < ActiveRecord::Base 
    searchkick 
    has_many :reviews , dependent: :destroy 
    has_many :taggings, dependent: :destroy 
    has_many :tags, through: :taggings 
    #Paperclip Installation 
    has_attached_file :image, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png" 
    validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"] 


    def all_tags=(names) 
    self.tags = names.split(",").map do |name| 
     Tag.where(name: name.strip).first_or_create! 
    end 
end 

def all_tags 
    self.tags.map(&:name).join(", ") 
end 

def self.tagged_with(name) 
    Tag.find_by_name!(name).posts 
end 

end 

模式

ActiveRecord::Schema.define(version: 20151026124712) do 

     create_table "posts", force: :cascade do |t| 
     t.string "title" 
     t.text  "description" 
     t.datetime "created_at",   null: false 
     t.datetime "updated_at",   null: false 
     t.integer "user_id" 
     t.string "tags" 
     t.string "image_file_name" 
     t.string "image_content_type" 
     t.integer "image_file_size" 
     t.datetime "image_updated_at" 
     end 

     create_table "reviews", force: :cascade do |t| 
     t.text  "comment" 
     t.datetime "created_at", null: false 
     t.datetime "updated_at", null: false 
     t.integer "user_id" 
     t.integer "post_id" 
     end 

     create_table "taggings", force: :cascade do |t| 
     t.integer "post_id" 
     t.integer "tag_id" 
     t.datetime "created_at", null: false 
     t.datetime "updated_at", null: false 
     end 

     add_index "taggings", ["post_id"], name: "index_taggings_on_post_id" 
     add_index "taggings", ["tag_id"], name: "index_taggings_on_tag_id" 

     create_table "tags", force: :cascade do |t| 
     t.string "name" 
     t.datetime "created_at", null: false 
     t.datetime "updated_at", null: false 
     end 

     create_table "users", force: :cascade do |t| 
     t.string "email",     default: "", null: false 
     t.string "encrypted_password",  default: "", null: false 
     t.string "reset_password_token" 
     t.datetime "reset_password_sent_at" 
     t.datetime "remember_created_at" 
     t.integer "sign_in_count",   default: 0, null: false 
     t.datetime "current_sign_in_at" 
     t.datetime "last_sign_in_at" 
     t.string "current_sign_in_ip" 
     t.string "last_sign_in_ip" 
     t.datetime "created_at" 
     t.datetime "updated_at" 
     t.string "name" 
     end 

     add_index "users", ["email"], name: "index_users_on_email", unique: true 
     add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true 

     create_table "votes", force: :cascade do |t| 
     t.integer "votable_id" 
     t.string "votable_type" 
     t.integer "voter_id" 
     t.string "voter_type" 
     t.boolean "vote_flag" 
     t.string "vote_scope" 
     t.integer "vote_weight" 
     t.datetime "created_at" 
     t.datetime "updated_at" 
     end 

     add_index "votes", ["votable_id", "votable_type", "vote_scope"], name: "index_votes_on_votable_id_and_votable_type_and_vote_scope" 
     add_index "votes", ["voter_id", "voter_type", "vote_scope"], name: "index_votes_on_voter_id_and_voter_type_and_vote_scope" 

    end 
+1

请不要链接图片,请将zur日志复制粘贴到问题中。这使得阅读起来更容易。 – spickermann

+0

向我们展示'db/schema.rb'文件。 –

+0

我也包括Schema和Post模型。 – AHmed

回答

1

由于错误日志中说,有在表的Tagging相关职位外键,所以它不允许你删除帖子。 我猜有许多引用的Tagging标记属于标签?在这种情况下,您需要删除所有属于您要删除的信息的标记。最简单的方法是添加dependent: :destroy到您的文章模型一样

# models/post.rb 
has_many :taggings, dependent: :destroy 
+0

我试过了,但仍然显示错误。我已经更新了我的问题,并且包含了模型文件和Schema。 – AHmed

1

你有Taggings表中的一些记录,其中记录Post引用。

所以,你有几种选择 -

  • has_many :taggings, dependent: :destroy

或者你可以改变你的迁移:

  • add_foreign_key :taggins, :posts, on_delete: :cascade(您可以在数据库迁移添加此)

Described here

+0

谢谢。我尝试了第一个选项,但它的错误仍然相同。你的意思是我在模式文件中添加的第二个选项? – AHmed

+0

我认为编辑Schema文件不是个好主意 - 只需将其添加到迁移文件 –