#前提・実現したいこと
new画面で画像をアップロードした際に画像が保存され、
その後、editアクションをrenderし、画像をアップロードしたテーブルにtitile、book_discも保存できるようにする。
Rails 5.2.4.1
ruby 2.6.1p33
・Book テーブルカラム情報
title:string
book_disc:string
picture:string
#発生している問題・エラーメッセージ
・画像アップロード時
Started POST "/users/1/books" for ::1 at 2020-03-02 23:47:03 +0900 Processing by BooksController#create as JSON Parameters: {"utf8"=>"✓", "authenticity_token"=>"puBrnLYgeeyU6tmtzB5mmkfevrCrwDBmQ5MGWr7JTYbMoBI0v2MqkxQS7XjWEkfE/5BWoTCDdTv67fVkLkb/5A==", "file"=>#, @original_filename="book_app_image3.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"file\"; filename=\"book_app_image3.png\"\r\nContent-Type: image/png\r\n">, "user_id"=>"1"} User Load (1.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]] ↳ vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.4.1/lib/active_record/log_subscriber.rb:98 Completed 500 Internal Server Error in 13ms (ActiveRecord: 1.6ms) ArgumentError (':edit' is not an ActiveModel-compatible object. It must implement :to_partial_path.): app/controllers/books_controller.rb:20:in `create'
・createボタン押下時
Started POST "/users/1/books" for ::1 at 2020-03-02 23:47:19 +0900 Processing by BooksController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"puBrnLYgeeyU6tmtzB5mmkfevrCrwDBmQ5MGWr7JTYbMoBI0v2MqkxQS7XjWEkfE/5BWoTCDdTv67fVkLkb/5A==", "title"=>"rails ", "book_disc"=>"rails sample", "commit"=>"Create", "user_id"=>"1"} User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]] ↳ vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.4.1/lib/active_record/log_subscriber.rb:98 Completed 500 Internal Server Error in 6ms (ActiveRecord: 0.4ms) ArgumentError (':edit' is not an ActiveModel-compatible object. It must implement :to_partial_path.): app/controllers/books_controller.rb:20:in `create'
#該当のソースコード
view/books/new.html.erb
<h1>Record new</h1> <!-- form_with url: user_books_path, local: true do |f| --> <!-- end --> <div class="book-new"> <%= form_with url: user_books_path, local: true do |f| %> <div class="form-group" align="left"> <%=f.label :title %><br> <%= f.text_field :title, value: "#{@book.title}", class: "form-control" %> </div> <div class="form-group" align="left"> <%= f.label :book_disc %><br> <%= f.text_area :book_disc, value: "#{@book.book_disc}", class: "form-control" %> </div> <div class="form-actions"> <%= f.submit "Create" %> </div> <% end %> <%= form_with url: user_books_path, class: 'dropzone', id: 'upload-dropzone', local: true do |f| %> <%= f.label :picture, class: "picture", align: "left" %> <div class="fallback"> <%= f.file_field('attach[:file]') %> </div> <% end %> </div> <div class="back-btn"> <%= link_to "Back", user_books_path, class: "back-btn" %> </div>
controllers/books_controller.rb
class BooksController < ApplicationController before_action :set_book, only: [:show, :edit, :update, :destroy] before_action :authenticate_user! before_action :ensure_correct_user, only: [:edit, :update, :destroy] def index @book = current_user.book.all end def all_user @book = Book.all end def new @book = Book.new end def create if @book = Book.new(picture: params[:file]) render partial: :edit else @book = Book.new(title: params[:title], book_disc: params[:book_disc], user_id: current_user[:id], picture: "default_book.jpg") end respond_to do |format| if @book.save format.html{redirect_to user_books_path, notice: 'Book was successfully created.'} else format.html{render :new} end end end def show end def edit end def update if @book.update(title: params[:title], book_disc: params[:book_disc]) redirect_to user_books_path(current_user), notice: 'Book was successfully updated.' else render partial: :edit end end def destroy @book.destroy redirect_to user_books_path(current_user), notice: "Book was successfully deleted." end private def book_params params.require(:book).permit(:title, :book_disc, :user_id) end def set_book @book = Book.find_by(id: params[:id]) end def ensure_correct_user if current_user.id != @book.user_id.to_i flash[:notice] = "You do not have permission" redirect_to user_books_path(current_user) end end end
db/schema.rb 一部抜粋
ActiveRecord::Schema.define(version: 2020_02_23_133409) do create_table "books", force: :cascade do |t| t.string "title" t.string "book_disc" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.integer "user_id" t.string "picture" end
#試したこと
下記サイトを参考に今回の実装作業をしました。
http://hinoshita.hatenadiary.com/entry/2016/10/11/223712
しかし、dropzoneは画像アップロード時にcreateが動作するため、title,book_discも併せて保存するため、createアクションはそれ用に修正しています。(しているつもりです、、)
もし、気になる点等ございましたら、コメントください。
また、情報不足ございましたら併せてコメントいただければ幸いです。
お手数ですがよろしくお願いいたします。
あなたの回答
tips
プレビュー