前提・実現したいこと
prototypeの部屋を作りたい
しかし保存しようとすると何も起きず
エラーも出ません
それをどうにかしたいです
発生している問題・エラーメッセージ
ruby on railsでの作成途中でおきました
エラーメッセージ
エラー文です
ActiveRecord::RecordInvalid in PrototypesController#create Validation failed: Image can't be blank Extracted source (around line #13): def create @prototype = Prototype.create(prototype_params) if @prototype.save! redirect_to root_path else render :new Rails.root: /Users/kamiyaryota/Desktop/projects/protospace-31035 Application Trace | Framework Trace | Full Trace app/controllers/prototypes_controller.rb:13:in `create' Request Parameters: {"authenticity_token"=>"X/fnWlXUYadtT1MdGuTVyinnpu2B/riCk3iqiFXD3HMV40FTl0eD+nyhOPxtSVP+LSwYDYjHDqkZ3HnYkVkLiQ==", "prototype"=> {"title"=>"aaa", "catct_copy"=>"aaa", "concept"=>"aaaa", "image"=> #<ActionDispatch::Http::UploadedFile:0x00007fe8ead15300 @content_type="image/png", @headers="Content-Disposition: form-data; name=\"prototype[image]\"; filename=\"test_image.png\"\r\n" + "Content-Type: image/png\r\n", @original_filename="test_image.png", @tempfile=#<File:/var/folders/4y/hbjp90hd2rl1bfd0m4kczr3m0000gn/T/RackMultipart20201023-27843-i4n9cr.png>>}, "commit"=>"保存する"} Toggle session dump Toggle env dump Response Headers: None
該当コード
routes.rbです
ruby
1Rails.application.routes.draw do 2 devise_for :users 3 root to: "prototypes#index" 4 resources :users, only: [:new, :edit, :update,] 5 resources :prototypes, only: [:new, :create] 6 7 devise_scope :user do 8 get '/users/sign_out' => 'devise/sessions#destroy' 9 end 10 11end 12
コントローラー.rbです
ruby
1class PrototypesController < ApplicationController 2 3 def index 4 5 end 6 7 def new 8 @prototype = Prototype.new 9 end 10 11 def create 12 @prototype = Prototype.*new*(prototype_params) 13 if @prototype.save 14 redirect_to root_path 15 else 16 render :new 17 end 18 end 19 20 21 22 23 private 24 def prototype_params 25 params.require(:prototype).permit(:title, :catct_copy, :concept).merge(user_id: current_user.id) 26 end 27 28end
model
prototype.rbです
ryby
1class Prototype < ApplicationRecord 2has_many :comments 3belongs_to :user 4 5has_one_attached :image 6 7validates :title, presence: true 8validates :catct_copy, presence: true 9validates :concept, presence: true 10validates :image, presence: true 11 12end
model
user.rbです
ruby
1class User < ApplicationRecord 2 # Include default devise modules. Others available are: 3 # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable 4 devise :database_authenticatable, :registerable, 5 :recoverable, :rememberable, :validatable 6 7 has_many :prototypes 8 has_many :comments 9 10 validates :name, presence: true 11 validates :profile, presence: true 12 validates :occupation, presence: true 13 validates :position, presence: true 14end
投稿ページです
html
1<div class="main"> 2 <div class="inner"> 3 <div class="form__wrapper"> 4 <h2 class="page-heading">新規プロトタイプ投稿</h2> 5 <%= render partial: "form" %> 6 </div> 7 </div> 8</div>
*部分テンプレートのコードです、ファイル名は_form_html_rbです*
html
1<%= form_with model: @prototype, local: true do |f|%> 2 <div class="field"> 3 <%= f.label :title, "プロトタイプの名称" %><br /> 4 <%= f.text_field :title %> 5 </div> 6 7 <div class="field"> 8 <%= f.label :catct_copy, "キャッチコピー" %><br /> 9 <%= f.text_area :catct_copy, class: :form__text %> 10 </div> 11 12 <div class="field"> 13 <%= f.label :concept, "コンセプト" %><br /> 14 <%= f.text_area :concept, class: :form__text %> 15 </div> 16 17 <div class="field"> 18 <%= f.label :image, "プロトタイプの画像" %><br /> 19 <%= f.file_field :image %> 20 </div> 21 22 <div class="actions"> 23 <%= f.submit "保存する", class: :form__btn %> 24 </div> 25<% end %>
routesの一覧です
Prefix Verb URI Pattern Controller#Action new_user_session GET /users/sign_in(.:format) devise/sessions#new user_session POST /users/sign_in(.:format) devise/sessions#create destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy new_user_password GET /users/password/new(.:format) devise/passwords#new edit_user_password GET /users/password/edit(.:format) devise/passwords#edit user_password PATCH /users/password(.:format) devise/passwords#update PUT /users/password(.:format) devise/passwords#update POST /users/password(.:format) devise/passwords#create cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel new_user_registration GET /users/sign_up(.:format) devise/registrations#new edit_user_registration GET /users/edit(.:format) devise/registrations#edit user_registration PATCH /users(.:format) devise/registrations#update PUT /users(.:format) devise/registrations#update DELETE /users(.:format) devise/registrations#destroy POST /users(.:format) devise/registrations#create root GET / prototypes#index new_user GET /users/new(.:format) users#new edit_user GET /users/:id/edit(.:format) users#edit user PATCH /users/:id(.:format) users#update PUT /users/:id(.:format) users#update prototypes POST /prototypes(.:format) prototypes#create new_prototype GET /prototypes/new(.:format) prototypes#new
回答1件
あなたの回答
tips
プレビュー