Ruby on Rails를 배우고 있는데이 문제가 있습니다. 나는 다른 유사한 문제를 보았지만 나에게 도움이되는 해결책을 찾지 못했다.NoMethodError in Books #
모든 것이 올바르게 작동했지만 사용자가 이미지를 선택할 수 있도록 f.file_field
을 추가했습니다. 이제 사용자가 이미지를 선택하면이 오류가 발생하지만 오류가 발생하지 않는다면 오류가 발생합니다.
이이 오류를
def new
@book = current_user.books.build
@categories = Category.all.map{ |c| [c.name, c.id] }
end
def create
@book = current_user.books.build(book_params)
@book.category_id = params[:category_id]
if @book.save
redirect_to root_path
else
render 'new'
end
end
를 일으키는 내가 생각하는 컨트롤러의 일부 도서
class Book < ApplicationRecord
belongs_to :user
belongs_to :category
has_attached_file :book_img, styles: { book_index: "250x350>", book_show: "325x475>" }, default_url: "/images/:style/missing.png"
validates_attachment_content_type :book_img, content_type: /\Aimage\/.*\z/
end
내 모델입니다 그리고 이것은 뷰
이다<%= simple_form_for @book, :html => { :multipart => true } do |f| %>
<%= select_tag(:category_id, options_for_select(@categories), :prompt => "Select a category") %>
<%= f.file_field :book_img %>
<%= f.input :title, label: "Book Title" %>
<%= f.input :description %>
<%= f.input :author %>
<%= f.button :submit %>
<% end %>
사용자가 도서 카테고리를 선택할 수있는 select_tag
에 오류가 표시되는 이유를 모르겠습니다.
루비 : 루비 2.2.6p396
레일 : 당신은 create
액션 내부 @categories
변수를 설정해야합니다 5.0.2
서버를 재시작하고 다시 시도하십시오. 또한'categories' 테이블에 레코드가 있습니까? – amrrbakry
서버를 여러 번 다시 시작했습니다. 그리고 나는 수동으로 카테고리를 추가했고 각 책에는 category_id가 있습니다. – CNuts
'strong params'에'book_img'가 있습니까? –