【问题标题】:Heroku routing/controller issue when going from dev to prod?从 dev 到 prod 时的 Heroku 路由/控制器问题?
【发布时间】:2015-12-06 17:12:15
【问题描述】:

我在 Heroku 上从本地开发到生产时遇到问题。除了我在“留言簿”控制器中的一个“新”操作外,一切都运行良好。当我尝试导航到 https://allbugsaside.herokuapp.com/guestbooks/new 时,我收到一条错误消息,告诉我查看日志。当我运行 Heroku 日志时,它并没有告诉我太多信息,只是发起了一个请求。

我感觉我可能遇到了问题,因为在我的“新”操作中,我在当前没有任何数据的留言簿上调用 .all?只是一个理论。

谁能帮助解决这个问题?谢谢。

class GuestbooksController < ApplicationController
    def index
        @guestbooks = Guestbook.all
    end

    def new
        @guestbook = Guestbook.new
        @guestbooks = Guestbook.all
        @guestbooks = Kaminari.paginate_array(@guestbooks).page(params[:page]).per(5)
    end

    def create
        @guestbook = Guestbook.new(guestbook_params)
        # @guestbooks = Guestbook.all.limit(1).page(params[:page])

        if @guestbook.save
            flash.now[:notice] = "Thanks for taking the time to write us! We greatly appreciate it!"
            render :new
        else
            flash.now[:notice] = "Your message failed to post, please try again"
            render :new
        end
    end

    private
    def guestbook_params
        params.require(:guestbook).permit(:name, :email, :message)
    end
end

这是我的 new.html.erb

<h1 class="guestbook_head">Guestbook</h1><br/>

<div class="guestbook_text">
    <p>
        It is our goal to provide our customers with the best possible Pest Control Services and Solutions in the industry.

        Your comments and feedback are important to us.  They are a tool we use in order to keep on improving our experience, with you ….the customer. <br/><br/>
    </p>
</div>

<div class="paginate">
    <%= paginate @guestbooks %>
</div>

<div class="span1">
    <% @guestbooks.each do |g| %>
        <br/>
        <h4><%= g.name %>, <%= g.created_at %><br/></h4>
        <%= g.message %><br/>
        <p>-----------------------------------------------------------------------------------------------------------------------</p>
    <% end %>
</div>

<%= simple_form_for @guestbook, url: guestbooks_path, method: :post do |f| %>
    <div class="span2"><%= f.input :name %></div>
    <div class="span2"><%= f.input :email %></div>
    <div class="span3"><%= f.input :message %></div>
    <div class="span4"><%= f.button :submit %></div><br/><br/><br/>
<% end %>

这些是我的路线;

Rails.application.routes.draw do
  resources :pages, only: [:index] 
  get "/pages/home", to: "pages#home"
  get "/pages/about", to: "pages#about"
  get "/pages/pest_info", to: "pages#pest_info"
  get "/pages/annual_contracts", to: "pages#annual_contracts"
  get "/pages/monthly_info", to: "pages#monthly_info"
  get "/pages/snowplow", to: "pages#snowplow"
  get "/pages/gallery", to: "pages#gallery"
  resources :contact_form, only: [:new, :create]
  resources :guestbooks, only: [:new, :create]
  get "/guestbooks/index", to: "guestbooks#index"
  root "pages#home"
end

当我尝试访问该页面时收到 500 内部服务器错误。

2015-12-06T18:42:33.507953+00:00 heroku[router]: at=info method=GET path="/pages/about" host=allbugsaside.herokuapp.com request_id=e07095da-734c-4fe3-aaa8-6f68d86a8e20 fwd="96.248.110.224" dyno=web.1 connect=0ms service=7ms status=200 bytes=5022
2015-12-06T18:42:40.331424+00:00 heroku[router]: at=info method=GET path="/guestbooks/new" host=allbugsaside.herokuapp.com request_id=134c9aab-f559-4e95-bad5-c1037dd38291 fwd="96.248.110.224" dyno=web.1 connect=0ms service=8ms status=500 bytes=1754
2015-12-06T18:42:40.390097+00:00 heroku[router]: at=info method=GET path="/guestbooks/new" host=allbugsaside.herokuapp.com request_id=d552b372-80fa-4e76-83a3-371aa4f994ab fwd="96.248.110.224" dyno=web.1 connect=0ms service=7ms status=500 bytes=1754

【问题讨论】:

  • 索引在您执行resources :guestbooks, only: [:new, :create, :index] 时可用,并且根据Rails 约定,您将通过url 访问索引:/guestbooks 或在任何视图内的link_to 代码中:guestbooks_path
  • 我明白你在说什么。很好地抓住了代码。但没有解决问题。

标签: ruby-on-rails heroku routing


【解决方案1】:

您是否已完成heroku run rake db:migrate 以更新您的数据库?当然,前提是您在本地环境中进行了新的迁移。如果这不能解决您的问题,请从 heroku 发布日志。

请记住,Rails 约定使用index(在您的控制器中查看和定义)来显示您想要从某个模型获得的所有对象。

在这里,您希望在new-object 视图中显示它们。这是可能的,只是要小心。你的观点叫new.html.erb 吗?

还要记住def new 不是一个动作,它只是你的new.html.erb 的一个控制器,它为所谓的视图提供数据、变量、重定向和其他内容。

【讨论】:

  • 我将我的 new.html.erb 模板和路由文件添加到了这个问题。我已经运行 heroku run rake db:migrate,但无济于事。 @JuanM。
  • 我不熟悉简单的形式,但试试这个:&lt;%= simple_form_for @guestbook do |f| %&gt; 没有更多参数。在 Rails 4 中,不需要指定其他内容,因为 @guestbook 是一个新实例,提交按钮会将操作引导到 create 方法。
【解决方案2】:

我注意到您的页面 /guestbooks 没有响应索引操作。你能发布路线.rb

Ruby 在技术上是如此动态:

 @guestbooks = Guestbook.all
 @guestbooks = Kaminari.paginate_array(@guestbooks).page(params[:page]).per(5)

但我不会这样做。重新定义一个变量并将其用作对声明的引用,就像这句话一样难以阅读。

@guestbooks = Kaminari.paginate_array(Guestbook.all).page(params[:page]).per(5)

如果问题存在于生产而不是开发中,则发布的代码不是问题。这不是数据库中缺少数据的问题,但是可能是架构的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-14
    • 1970-01-01
    • 2016-08-08
    • 1970-01-01
    • 1970-01-01
    • 2011-05-09
    • 2021-08-12
    相关资源
    最近更新 更多