編集画面に飛ぶ際にエラーがおきます
フォームオブジェクトにてtagsとbooksを結びつけてからこのようなエラーが起きました
bookコントローラー
部分でエラー
class BooksController < ApplicationController before_action :search_book, only: [:index, :search] def new @book = BooksTag.new end def index @books =Book.all.order(created_at: :desc) set_book_column end def show @book = Book.find(params[:id]) end def destroy book = BooksTag.find(params[:id]) book.destroy redirect_to books_path end def create # @book = Book.new(book_params) @book = BooksTag.new(book_params) if @book.valid? @book.save redirect_to root_path else render 'new' end end def search @results = @p.result @books =Book.all.order(created_at: :desc) end def edit @book = BooksTag.find(params[:id]) end def update book = BooksTag.find(params[:id]) book.update(book_params) redirect_to root_path end private def book_params params.require(:books_tag).permit(:name, :content,:genre_id,:image,:tag_name).merge(user_id: current_user.id) end def search_book @books =Book.all @p = Book.ransack(params[:q]) end def set_book_column @book_name = Book.select("genre_id").distinct # 重複なくnameカラムのデータを取り出す end end
tagとbookをまとめているformオブジェクト
class BooksTag include ActiveModel::Model attr_accessor :content, :name,:genre_id,:image,:user_id,:tag_name with_options presence: true do validates :content validates :name validates :genre_id validates :tag_name validates :image end def save book = Book.create(content: content,name: name,genre_id: genre_id,image: image,user_id: user_id) tag = Tag.create(tag_name: tag_name) tag = Tag.where(tag_name: tag_name).first_or_initialize tag.save tag_relation =TagRelation.create(book_id: book.id, tag_id: tag.id) tag_relation.save end end
モデル関係
book.rb has_many :tag_relations has_many :tags, through: :tag_relations tag.rb class Tag < ApplicationRecord has_many :tag_relations has_many :books, through: :tag_relations end tag.relation.rb class TagRelation < ApplicationRecord belongs_to :book belongs_to :tag end
book edit.html.erb
<body><div class="container"> <div class="sec1title"> <h2>*画像を投稿!*</h2> </div> <%= form_for (@book) do |f| %> <div class="field" type="text"> <%= f.label :name, "本の名前" %><br /> <%= f.text_field :name %> </div> <div class="tag-field", id='tag-field'> <%= f.label :tag_name, "タグ" %> <%= f.text_field :tag_name, placeholder: "検索されやすいキーワードを入れましょう" %> </div> <div class="genre" > <%= f.label :genre_id, "本のジャンル" %><br /> <%= f.collection_select(:genre_id,Genre.all,:id,:type,{prompt: "---"},{class:"select-box"}) %> </div> <div class="form-input"> <div class="text-image-content"> <%= f.label :content, "説明文" %><br /> <%= f.text_area :content, placeholder: "話したい内容について、ご自由にお書きください" , rows: "12" %> </div> <br /><br /> <label class="form-image"> <span class="image-file">画像</span> <%= f.file_field :image %> </label> <div class="image-stage", id="image-list"></div> </div> <div class="actions"> <%= f.submit "更新", class: "btn1" %> </div> <% end %> </div> </body>
findをallにしてりcreateしてみたが違うエラーが出ます
https://gyazo.com/5ea5d098c2275b2377d9755baf901af7
`
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/08/12 07:48
2020/08/12 08:10