【问题标题】:Heroku Error Deleting a Post in Production: RailsHeroku 错误删除生产中的帖子:Rails
【发布时间】:2016-03-02 09:09:21
【问题描述】:

我正在尝试删除我应用中的帖子。它在 localhost 中运行良好,但是当我推送到 heroku 时它无法正常工作。我收到一条错误消息,提示“出现问题,请检查日志”。这是我的代码:

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

日志

查看/posts/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>

models/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

【问题讨论】:

  • 请不要链接图片,只需将 zur 日志复制并粘贴到问题中即可。这使它更容易阅读。
  • 向我们展示db/schema.rb 文件。
  • 我也包含了 Schema 和 Post 模型。

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4 heroku


【解决方案1】:

Taggings 表中有一些记录引用了Post 记录。

所以你有几个选择 -

  • has_many :taggings, dependent: :destroy

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

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

Described here

【讨论】:

  • 谢谢。我尝试了第一个选项,但错误仍然相同。你的意思是我将第二个选项添加到架构文件中??
  • 我认为编辑 Schema 文件不是一个好主意 - 只需将其添加到迁移文件中
【解决方案2】:

正如错误日志所说,taggings 表中有与帖子相关的外键,因此它不允许您删除帖子。 我猜一个post有很多taggings,而一个tagging属于一个tag?在这种情况下,您需要删除属于您要删除的帖子的所有标签。最简单的方法是将dependent: :destroy 添加到您的帖子模型中,例如

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

【讨论】:

  • 我试过了,但仍然显示错误。我已经更新了我的问题并包含了模型文件和架构。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-02
  • 2017-08-03
  • 1970-01-01
相关资源
最近更新 更多