繁体   English   中英

Rails heroku ActionView::Template::Error(未定义的方法`author'

[英]Rails heroku ActionView::Template::Error (undefined method `author'

这在本地有效,但在 heroku 中无效。

我试过在本地迁移,然后推送到 heroku 它没有用。 我试过 heroku run rake db:migrate,然后重新启动它没有用。

我试图重新启动 dyno,它也没有工作。

有什么我可以检查的东西会提供更多细节吗? 我正在查看 heroku run rake db:migrate:status,但它只显示一般迁移创建书籍。

错误:

 2017-09-20T21:31:06.432739+00:00 app[web.1]: I, [2017-09-20T21:31:06.432588 #4]  INFO -- : [f953f5f7-3cab-45a4-9f97-2cd7004e2807] Started GET "/books" for 81.106.178.87 at 2017-09-20 21:31:06 +0000
2017-09-20T21:31:06.441798+00:00 app[web.1]: I, [2017-09-20T21:31:06.441609 #4]  INFO -- : [f953f5f7-3cab-45a4-9f97-2cd7004e2807]   Rendering books/index.html.erb within layouts/application
2017-09-20T21:31:06.435519+00:00 app[web.1]: I, [2017-09-20T21:31:06.435371 #4]  INFO -- : [f953f5f7-3cab-45a4-9f97-2cd7004e2807] Processing by BooksController#index as HTML
2017-09-20T21:31:06.487993+00:00 app[web.1]: D, [2017-09-20T21:31:06.487768 #4] DEBUG -- : [f953f5f7-3cab-45a4-9f97-2cd7004e2807]   Book Load (4.1ms)  SELECT "books".* FROM "books"
2017-09-20T21:31:06.549929+00:00 app[web.1]: I, [2017-09-20T21:31:06.549786 #4]  INFO -- : [f953f5f7-3cab-45a4-9f97-2cd7004e2807]   Rendered books/index.html.erb within layouts/application (107.8ms)
2017-09-20T21:31:06.550338+00:00 app[web.1]: I, [2017-09-20T21:31:06.550261 #4]  INFO -- : [f953f5f7-3cab-45a4-9f97-2cd7004e2807] Completed 500 Internal Server Error in 115ms (ActiveRecord: 22.9ms)
2017-09-20T21:31:06.552408+00:00 app[web.1]: F, [2017-09-20T21:31:06.552334 #4] FATAL -- : [f953f5f7-3cab-45a4-9f97-2cd7004e2807]   
2017-09-20T21:31:06.552538+00:00 app[web.1]: F, [2017-09-20T21:31:06.552475 #4] FATAL -- : [f953f5f7-3cab-45a4-9f97-2cd7004e2807] ActionView::Template::Error (undefined method `author' for #<Book:0x00000003849768>):
2017-09-20T21:31:06.552872+00:00 app[web.1]: [f953f5f7-3cab-45a4-9f97-2cd7004e2807]     24:         <tr>
2017-09-20T21:31:06.552875+00:00 app[web.1]: [f953f5f7-3cab-45a4-9f97-2cd7004e2807]     27:             <td><%=  image_tag book.image_url.to_s%></td>
2017-09-20T21:31:06.552870+00:00 app[web.1]: F, [2017-09-20T21:31:06.552781 #4] FATAL -- : [f953f5f7-3cab-45a4-9f97-2cd7004e2807]     23:       <% @books.each do |book| %>
2017-09-20T21:31:06.552873+00:00 app[web.1]: [f953f5f7-3cab-45a4-9f97-2cd7004e2807]     25:             <td><%= book.name %></td>
2017-09-20T21:31:06.552874+00:00 app[web.1]: [f953f5f7-3cab-45a4-9f97-2cd7004e2807]     26:             <td><%= book.author %></td>
2017-09-20T21:31:06.552876+00:00 app[web.1]: [f953f5f7-3cab-45a4-9f97-2cd7004e2807]     28:             <td><%= link_to "Свали Книга", book.attachment_url %></td>
2017-09-20T21:31:06.552881+00:00 app[web.1]: [f953f5f7-3cab-45a4-9f97-2cd7004e2807]     29:             <td><%= button_to "Delete", book, method: :delete, class: "btn btn-danger", confirm: "Сигурни ли сте, че желаете да изтриете #{book.name}?" %></td>
2017-09-20T21:31:06.552931+00:00 app[web.1]: F, [2017-09-20T21:31:06.552862 #4] FATAL -- : [f953f5f7-3cab-45a4-9f97-2cd7004e2807]   
2017-09-20T21:31:06.553046+00:00 app[web.1]: F, [2017-09-20T21:31:06.552958 #4] FATAL -- : [f953f5f7-3cab-45a4-9f97-2cd7004e2807] app/views/books/index.html.erb:26:in `block in _app_views_books_index_html_erb__879124352395591768_27663240'
2017-09-20T21:31:06.553047+00:00 app[web.1]: [f953f5f7-3cab-45a4-9f97-2cd7004e2807] app/views/books/index.html.erb:23:in `_app_views_books_index_html_erb__879124352395591768_27663240'

db/migrate/create_books

class CreateBooks < ActiveRecord::Migration[5.1]
  def change
    create_table :books do |t|
      t.string :name
      t.string :author
      t.string :image
      t.string :attachment

      t.timestamps
    end
  end
end

模式文件

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

  create_table "books", force: :cascade do |t|
    t.string "name"
    t.string "author"
    t.string "image"
    t.string "attachment"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

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

end

书籍/index.html.erb

<% if !flash[:notice].blank? %>
    <div class="alert alert-info">
        <%= flash[:notice] %>
    </div>
<%end%>

<br />

<%= link_to "Нова Книга", new_book_path, class: "btn btn-primary mb1 bg-blue" %>
<br />
<br />
<table class="table table-boarded table-striped">
    <thread>
        <tr>
            <th>Име</th>
            <th>Aвтор</th>
            <th>Корица</th>
            <th>Линк за сваляне</th>
            <th>&nbsp;</th>
        </tr>
    </thread>
    <tbody>
        <% @books.each do |book| %>
        <tr>
            <td><%= book.name %></td>
            <td><%= book.author %></td>
            <td><%=  image_tag book.image_url.to_s%></td>
            <td><%= link_to "Свали Книга", book.attachment_url %></td>
            <td><%= button_to "Delete", book, method: :delete, class: "btn btn-danger", confirm: "Сигурни ли сте, че желаете да изтриете #{book.name}?" %></td>
        </tr>
        <% end %>
    </tbody>
</table>

控制器/books_controller.rb

class BooksController < ApplicationController
  def index
    @books = Book.all
  end

  def new
    @book = Book.new
  end 

  def create
    @book = Book.new(book_params)

    if @book.save
        redirect_to books_path, notice: "Kнигата '#{@book.name}' е качена успешно!"
    else
        render 'new'
    end
  end

  def destroy
    @book = Book.find(params[:id])

    @book.destroy

    redirect_to books_path, notice: "Книгата е изтрита!"
  end

  private

  def book_params
    params.require(:book).permit(:name, :author, :image, :attachment)
  end

end

我能够让它运行,这是有帮助的答案。

https://stackoverflow.com/a/32159297/7264254

也许您在最初运行 heroku run rake db:migrate 之后添加了验证码列,其中包括该迁移? 如果是这种情况,您需要使用以下命令重置数据库(请注意,这将清除所有数据)

heroku pg:reset DATABASE 然后再次迁移它

heroku run rake db:migrate 在这些之后,验证码也应该在 Heroku 上可用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM