前提
コントローラーでrequireを設定するとエラーが出ます。
実現したいこと
投稿した画像にタグ付けをしたいです。
発生している問題・エラーメッセージ
ActionController::ParameterMissing in DesignsController#create param is missing or the value is empty: post_form
該当のソースコード
ruby
1[app>controllers>designs_controller.rb] 2 3class DesignsController < ApplicationController 4 before_action :authenticate_user!, only:[:edit, :new, :destroy] 5 before_action :move_to_index, except: [:index, :show] 6 before_action :set_design, only: [:show, :edit] 7 8 def index 9 @designs = Design.includes(:user).order("created_at DESC") 10 end 11 12 def new 13 @designs = Design.new 14 end 15 16 def create 17 @designs = Design.create(design_params) 18 if 19 @designs.save 20 redirect_to root_path 21 else 22 render :new 23 end 24 end 25 26 def show 27 28 end 29 30 def destroy 31 design = Design.find(params[:id]) 32 if design.destroy 33 redirect_to root_path 34 end 35 end 36 37 def update 38 design = Design.find(params[:id]) 39 if 40 design.update(design_params) 41 redirect_to design_path(design.id) 42 else 43 redirect_to request.referer 44 end 45 end 46 47 def edit 48 unless user_signed_in? && current_user.id == @design.user_id 49 redirect_to action: :index 50 end 51 end 52 53 def search 54 @designs = Design.search(params[:keyword]).order("created_at DESC") 55 end 56 57 private 58 59 def design_params 60 params.require(:post_form).permit(:title, :file_name, :image).merge(user_id: current_user.id) 61 end 62 63 def set_design 64 @design = Design.find(params[:id]) 65 end 66 67 def move_to_index 68 unless user_signed_in? 69 redirect_to action: :index 70 end 71 end 72 73end
ruby
1[app>models>post_form.rb] 2 3class PostForm 4 include ActiveModel::Model 5 6 attr_accessor( 7 :title, :file_name, :image, 8 :id, :created_at, :updated_at, 9 :tag_name 10 ) 11 12 with_options presence: true do 13 validates :title 14 validates :file_name 15 validates :image 16 end 17 18 def save 19 designs = Design.create(text: text, image: image) 20 tag = Tag.where(tag_name: tag_name).first_or_initialize 21 tag.save 22 PostTagRelation.create(design_id: design.id, tag_id: tag.id) 23 end 24
ruby
1[app>models>post_tag_relation.rb] 2class PostTagRelation < ApplicationRecord 3 belongs_to :design 4 belongs_to :tag 5end
ruby
1[app>models>design.rb] 2 3class Design < ApplicationRecord 4 belongs_to :user 5 has_many :post_tag_relations 6 has_many :tags, through: :post_tag_relations 7 has_one_attached :image 8 9 validates :title, presence: true 10 validates :file_name, presence: true 11 validates :image, presence: true 12 13 def self.search(search) 14 if search != "" 15 Design.where('title LIKE(?)', "%#{search}%") 16 else 17 Design.all 18 end 19 end 20 21 22 23end
ruby
1[app>views>show.html.erb] 2 3<main class="main"> 4 <div class="inner"> 5 <div class="prototype__wrapper"> 6 <p class="prototype__hedding"> 7 <%= @design.title %> 8 </p> 9 <% if user_signed_in? && current_user.id == @design.user_id %> 10 <div class="prototype__detail"><br> 11 <p class="detail__title">ファイル場所</p> 12 <p class="detail__message"> 13 <%= @design.file_name %> 14 </p> 15 </div> 16 <% end %> 17 18 <span class="tag_style"><%= @design.tags.first&.tag_name %></span> 19 20 <div class="prototype__image"> 21 <%= image_tag @design.image %> 22 </div> 23 </div> 24 </div> 25</main>
ruby
1[app>views>designs>_form.html.erb] 2<%= form_with model: @design, url: url, method: method, id: 'new_post', local: true do |f| %> 3 4 <div class="field"> 5 <%= f.label :title, "タイトル" %><br /> 6 <%= f.text_field :title, id:"design_title" %> 7 </div> 8 9 <div class="field"> 10 <%= f.label :file_name, "ファイル名" %><br /> 11 <%= f.text_area :file_name, class: :form__text, id:"design_file_name" %> 12 </div> 13 14 <div class="field"> 15 <%= f.label :image, "画像" %><br /> 16 <%= f.file_field :image, id:"design_image" %> 17 </div> 18 19 <div class="tag-field"> 20 <%= f.text_field :tag_name, placeholder: 'add tags' %> 21 </div> 22 23 <div class="actions"> 24 <%= f.submit "保存する", class: :form__btn %> 25 </div> 26<% end %>
試したこと
requireを削除すると正常に表示されるのですが
今度はタグの保存・表示ができなくなります。
補足情報(FW/ツールのバージョンなど)
初心者でして、初歩的な質問かと思いますが、何卒宜しくお願いいたします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。