2016-08-31 231 views
0

我找不到属性: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做 资源:书籍 根 '的书#指数' 结束'

+0

我想你已经试图通过你的'BooksController'从表单创建一本书。你可以显示已经传递给你的控制器的参数吗?您可以在应用程序的日志文件中找到它们。 – Stefan

+2

尝试创建新记录,甚至可以看到没有描述。 from Console –

+0

您正在使用哪个版本的Rails?你有没有在Gemfile中声明的“protected_attributes”gem?你的'Book'模型中有“attr_accessible”吗? – romainsalles

回答

0

检查PARAMS哈希通过创建行动创造的书。我认为描述会丢失。

你可以在这里粘贴params散列吗?

+0

这是一条评论而不是答案。 – engineersmnky

+0

我可以在哪里检查params哈希?你的意思是这样的: “DEF book_params \t params.require(:书).permit(:描述:标题:作者) 结束” –

+0

没有,如果你正在运行在开发模式在本地的应用程序,然后检查您的Rails服务器正在运行的日志。当你提交表格时,日志会在那里生成。在那里你可以看到传递给该动作的params散列。 – prashant