私は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
のRails:Railsの5.0.2
サーバーを再起動してもう一度お試しください。また、 'categories'テーブルにレコードがありますか? – amrrbakry
私はサーバを何度も再起動しました。そして私は手動でカテゴリを追加しました。各ブックはcategory_idを持っています – CNuts
'strong params'に' book_img'がありますか? –