为什么属性:description返回nil

时间:2016-08-31 15:29:51

标签: ruby-on-rails ruby null return

我找不到属性:description返回nil的错误。 来自rails console

@book = Book.first
# Book Load (0.7ms)  SELECT  "books".* FROM "books"   ORDER BY "books"."id" ASC LIMIT 1
#=> #<Book id: 1, title: "A Game of Thrones", description: nil, author: "George R. R. Martin ", created_at: "2016-09-01 13:27:09", updated_at: "2016-09-01 13:27:09"> 

books_controller.rb

class BooksController < ApplicationController

  before_action :find_book, only: [:show, :edit, :update, :destroy]

  def index
    @books = Book.all.order("created_at DESC")
  end

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

  def new
    @book = Book.new
  end

  def create
    @book = Book.new(book_params)

    if @book.save
      redirect_to root_path
    else
      render 'new'
    end
  end

  private

  def book_params
    params.require(:book).permit(:description, :title,  :author)
  end

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

end

_form.html.erb

<%= f.input :title, label: "Book Title" %>
<%= f.input :description %>
<%= f.input :author %>
<%= f.button :submit %>

'show.html.erb'

<h2><%= @book.title %></h2>
<h3><%= @book.author %></h3>
<h2><%= @book.description %></h2>

routs.rb

'Rails.application.routes.draw做   资源:书籍   root'books #index' 端“

1 个答案:

答案 0 :(得分:0)

检查传递的params hash以创建创建book的动作。我认为描述会丢失。

你可以在这里粘贴params哈希吗?