Ruby on rails RubyonRails:未定义的方法&map';对于nil:NilClass,这是什么原因?当我将记录添加到表中时
这是我的_form.html.erbRuby on rails RubyonRails:未定义的方法&map';对于nil:NilClass,这是什么原因?当我将记录添加到表中时,ruby-on-rails,ruby,Ruby On Rails,Ruby,这是我的_form.html.erb <%= simple_form_for @book,:html => { :multipart => true } do |f| %> <%= select_tag(:category_id, options_for_select(@categories), :promt => "Select a category") %> <%= f.file_field :book_img %> <%= f.
<%= simple_form_for @book,:html => { :multipart => true } do |f| %>
<%= select_tag(:category_id, options_for_select(@categories), :promt => "Select a category") %>
<%= f.file_field :book_img %>
<%= f.input :title, label: "Book Title" %>
<%= f.input :description %>
<%= f.input :author %>
<%= f.button :submit %>
{:multipart=>true}do | f |%>
“选择类别”)%%>
这是我的图书控制器
class BooksController < ApplicationController
before_filter :initialize_book
before_action :find_book, only: [:show, :edit, :update, :destroy]
def initialize_book
@book = Book.new
end
def show
@book =Book.find(params[:id])
end
def index
if params[:category].blank?
@books = @books = Book.all.order("created_at DESC")
else
@category_id = Category.find_by(name: params[:category]).id
@books = Book.where(:category_id => @category_id).order("created_at DESC")
end
end
def new
@book = current_user.books.build # Book.new
@categories = Category.all.map{ |c| [c.name, c.id]}
end
def create
@book =current_user.books.build(book_params) # Book.new(book_params)
@book.category_id = params[:category_id]
if @book.save
redirect_to root_path
else
render 'new'
end
end
def edit
@categories = Category.all.map{ |c| [c.name, c.id]}
end
def update
@book.category_id = params[:category_id]
if @book.update(book_params)
redirect_to book_path(@book)
else
render 'edit'
end
end
def destroy
@book.destroy
redirect_to root_path
end
private
def book_params
params.require(:book).permit(:title, :description, :author, :category_id, :book_img)
end
def find_book
@book =Book.find(params[:id])
end
class BooksController@category\u id).order(“在DESC创建”)
结束
结束
def新
@book=当前_user.books.build#book.new
@categories=Category.all.map{| c |[c.name,c.id]}
结束
def创建
@book=当前用户.books.build(book_参数)#book.new(book_参数)
@book.category\u id=参数[:category\u id]
如果@book.save
将\重定向到根\路径
其他的
呈现“新”
结束
结束
定义编辑
@categories=Category.all.map{| c |[c.name,c.id]}
结束
def更新
@book.category\u id=参数[:category\u id]
如果@book.update(book_参数)
将_重定向到book_路径(@book)
其他的
渲染“编辑”
结束
结束
def销毁
@毁灭
将\重定向到根\路径
结束
私有的
def book_参数
参数require(:book).permit(:title,:description,:author,:category\u id,:book\u img)
结束
def查找书
@book=book.find(参数[:id])
结束
结束
book.rb
classbook”,book\u show:“325x475>”},默认url:“/images/:style/missing.png”
验证\附件\内容\类型:图书\图片,内容\类型:/\Aimage\/.\z/
结束
您仅为编辑操作设置了类别,但使用“新建”又如何?
试试这个:
before_action :set_categories, only: [:new,:create, :edit]
private
def set_categories
@categories = Category.all.map{ |c| [c.name, c.id]}
end
您仅为“编辑”操作设置类别,但“新建”又如何? 试试这个:
before_action :set_categories, only: [:new,:create, :edit]
private
def set_categories
@categories = Category.all.map{ |c| [c.name, c.id]}
end
这一行
app/controllers/books\u controller.rb:39:in create'
表示方法create
中的错误。在create
中,不设置@类别
。
您的before筛选器和操作仅设置为@book
render:new
不会执行new
方法。它仅为新建的呈现给定模板。所以@categories
仍然是nil
这一行app/controllers/books\u controller.rb:39:in create'
表示方法create
中的错误。在create
中,不设置@类别
。
您的before筛选器和操作仅设置为@book
render:new
不会执行new
方法。它仅为新建的呈现给定模板。所以@categories
仍然是nil