簡體   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