【问题标题】:Can't add new post in heroku after heroku run rake db:migrateheroku run rake db:migrate 后无法在 heroku 中添加新帖子
【发布时间】:2014-06-05 18:06:51
【问题描述】:

我最近在 heroku 中上传了我的 Rails App。我有 3 个模型 -

  • 用户
  • 发布
  • 区

我执行了将应用程序部署到 heroku 所需的步骤。我的设计用户模型和地区模型 CRUD 工作正常。所以我想我在heroku中的数据库迁移工作得很好。但是当我尝试在 heroku 中写一篇新文章时,它给了我错误“我们很抱歉,但出了点问题。”。我可以在本地主机上写新帖子而不会出现任何错误。

posts_controller.rb

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

    # GET /posts
    # GET /posts.json
    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
    end

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

    # GET /posts/1/edit
    def edit
    end

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

      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 @post, 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 posts_url, 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, :tag_list, :location, :body, :user_id, :published)
      end
  end

_form.html.erb

    <%= form_for(@post) do |f| %>
      <% if @post.errors.any? %>
        <div id="error_explanation">
          <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>

          <ul>
          <% @post.errors.full_messages.each do |message| %>
            <li><%= message %></li>
          <% end %>
          </ul>
        </div>
      <% end %>

      <%= f.hidden_field :user_id, value: current_user.id %>

      <div class="field">
        <%= f.label :title %><br>
        <%= f.text_field :title %>
      </div>
      <div class="field">
        <%= f.label :tag_list, 'Insert Tags' %><br>
        <%= f.text_field :tag_list %>
      </div>
      <div class="field">
        <%= f.label :location %><br>
        <%= f.text_field :location %>
      </div>
      <div class="field">
        <%= f.label :body %><br>
        <%= f.text_area :body %>
      </div>
      <div class="field">
        <%= f.label :published %><br>
        <%= f.check_box :published %>
      </div>
      <div class="actions">
        <%= f.submit %>
      </div>
    <% end %>

【问题讨论】:

  • 我刚刚测试了应用程序,可以确认错误。您的代码看起来不错 - 那么问题是您有我们可以看到的任何日志吗?您可以通过在控制台中输入 heroku logs --trace 或安装 LogEntries 插件来完成此操作
  • "heroku logs --trace" 给了我错误'!无效参数:"--trace"'。所以我只运行“heroku 日志”并想出了这个日志pastebin.com/raw.php?i=xbBkK934。
  • Sorrrry - 我还是有点困!
  • 谢谢,我想那太好了!
  • 没问题 - 让我为你写吧!

标签: ruby-on-rails heroku


【解决方案1】:

为了响应您的 cmets,您最好将错误保存到数据库中,以便查看实际异常。为此,您基本上需要一个名为errors 的数据库,一些middleware 然后调用:

$ heroku rails c
$ errors = Error.last #-> shows latest error
$ errors.inspect()

问题

我个人认为你的问题是你试图用Post.create 方法做一些古怪的事情。具体来说,我相信您的model 中可能存在问题,导致保存机制在不引发异常的情况下完成。

如前所述,您的 controller 代码看起来不错 - 因此问题显然出在您应用的另一部分(通常是 model 或 db)。为了解决这个问题,我会查看您的模型,然后确保您的数据库具有所需的列,以便您正确保存数据。

...


例外情况

捕捉异常对你来说是件好事。

您可以通过两种方式做到这一点:

  • 将错误保存到您的数据库中
  • 将错误发送到您的电子邮件

我已经制作了一个 gem 用于将错误发送到您的数据库,但是您肯定会从中受益(这是我们在生产中使用的)是使用 exception_notification,这是一个维护的 gem,可以帮助您将异常发送到您的电子邮件:

#Gemfile
gem 'exception_notification', '~> 4.0.1'

#config/environments/production.rb
#Use Gmail's server for now
config.action_mailer.smtp_settings = {
  :address              => "smtp.gmail.com",
  :port                 => 587,
  :domain               => "domain.com",
  :user_name            => "your_name@gmail.com",
  :password             => Rails.application.secrets.gmail,
  :authentication       => :plain,
  :enable_starttls_auto => true
}  

config.middleware.use ExceptionNotification::Rack,
  :email => {
    :email_prefix => "Error",
    :sender_address => %{"notifier" <error@xxxxxxxxxxxx.com>},
    :exception_recipients => %w{support@xxxxxxxxxxxxxxx.com}
}

这应该将错误堆栈跟踪发送到您的电子邮件(我们在生产中使用它,所以它也应该适用于您)

--

宝石

我们实际上已经制作了一个名为 exception_handler (github repo) 的 gem ——如果你把它放到你的应用程序中,它可以让你将错误保存到你的数据库中(它还会在生产,它应该告诉你错误是什么):

#Gemfile
gem 'exception_handler'

#cmd
$ rails generate exception_handler:install #-> creates initializer
$ rails generate exception_handler:migration '-> creates "errors" db
$ rake db:migrate

#config/initializers/exception_handler.rb

#DB - 
#Options = false / true
config.db = true #-> set to true

#Social
    #Change these if you want
config.social = {
    twitter:    "http://twitter.com/frontlineutils",
    facebook:   "https://facebook.com/frontline.utilities",
    linkedin:   "https://linkedin.com/company/frontline-utilities",
    youtube:    "https://youtube.com/user/frontlineutils",
    fusion:     "http://frontlinefusion.com/frontlineutils"
}

这应该可以为您显示错误,然后我们可以更好地处理它们:)

【讨论】:

  • 好的,我会告诉你尝试这个之后会发生什么。感谢所有帮助。
  • 没问题!当你看不到发生了什么时,这很烦人
  • 好的,我尝试了这两种方法,如果“将错误发送到您的电子邮件”,则没有邮件发送到我指定的电子邮件(我也检查了垃圾邮件)。也许我做错了什么,请看这个pastebin.com/raw.php?i=0CP8k3CL
  • 当我在 heroku rails 控制台中运行“errors = Error.last”时,如果“将错误保存到您的数据库”会返回“NameError: uninitialized constant Error”。似乎我必须初始化一个错误类并将其加载到我的环境中。如果是这样,如何创建Error.rb?
  • 你需要使用ExceptionHandler::Error,对不起
【解决方案2】:

在 Heroku 上运行迁移时,这种情况经常发生在我身上。我建议运行heroku pg:reset DATABASE。在 Heroku 中,您无权作为分配给您的 PostgreSQL 用户删除和创建数据库。但运行此命令将通过 Heroku 而不是 PostgreSQL 方式为您运行这些确切的步骤。

查看this 了解更多信息。

【讨论】:

  • 无赖,如果我想到其他任何事情,我会告诉你的。让我们了解 Rich Peck 建议的结果!
【解决方案3】:

有时(只是有时)重启服务器有效:heroku restart

【讨论】:

  • 啊,我想这不是那种问题。无论如何,我尝试重新启动服务器,但它没有工作。感谢您的建议。
猜你喜欢
  • 2014-07-10
  • 1970-01-01
  • 2014-03-21
  • 2017-01-07
  • 2015-02-20
  • 1970-01-01
  • 1970-01-01
  • 2017-07-30
相关资源
最近更新 更多