質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

1回答

702閲覧

Formオブジェクトを用いて作成したデータを、編集・更新できるようにしたいです

validate

総合スコア0

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2021/07/21 08:45

前提・実現したいこと

Formオブジェクトを使ってタグ付け機能を行ったのですが編集ができなくなっています。
どうしたらいいですか俺がいします。

発生している問題・エラーメッセージ

ActionController::ParameterMissing in PostsController#update param is missing or the value is empty: posts_tag def update_params エラー文 params.require(:posts_tag).permit(:title, :text, :name).merge(user_id: current_user.id, post_id: params[:id]) end def set_post

該当のソースコード

posts_controller

ruby

1class PostsController < ApplicationController 2 before_action :set_post, only: [:edit, :show, :update, :destroy] 3 before_action :move_to_index, except: [:index, :show, :search] 4 5 def index 6 @posts = Post.includes(:user).order(created_at: :desc) 7 end 8 9 def new 10 @poststag = PostsTag.new 11 end 12 13 def create 14 @poststag = PostsTag.new(post_params) 15 if #@poststag.valid? 16 @poststag.save 17 return redirect_to root_path 18 else 19 render "new" 20 end 21 end 22 23 def destroy 24 @post.destroy 25 end 26 27 def edit 28 @poststag = PostsTag.new(title: @post.title, text: @post.text) 29 end 30 31 def update 32 @poststag = PostsTag.new(update_params) 33 if @poststag.valid? 34 @poststag.update 35 redirect_to root_path 36 else 37 render :edit 38 end 39 end 40 41 def show 42 @poststag = PostsTag.new(title: @post.title, text: @post.text) 43 @comment = Comment.new 44 @comments = @post.comment.includes(:user) 45 end 46 47 def search 48 @posts = Post.search(params[:keyword]) 49 end 50 51 private 52 53 def post_params 54 params.require(:posts_tag).permit(:title, :text, :name).merge(user_id: current_user.id) 55 end 56 57 def update_params 58 params.require(:posts_tag).permit(:title, :text, :name).merge(user_id: current_user.id, post_id: params[:id]) 59 end 60 61 def set_post 62 @post = Post.find(params[:id]) 63 end 64 65 def move_to_index 66 unless user_signed_in? 67 redirect_to action: :index 68 end 69 end 70end

model/posts_tag

ruby

1class PostsTag 2 3 include ActiveModel::Model 4 attr_accessor :text,:title,:user_id, :name 5 6 with_options presence: true do 7 validates :name 8 validates :text 9 validates :title 10 validates :user_id 11 end 12 13 delegate :persisted?, to: :post 14 15 def initialize(attributes = nil, post: Post.new) 16 @post = post 17 attributes ||= default_attributes 18 super(attributes) 19 end 20 21 22 def save 23 return if invalid? 24 25 ActiveRecord::Base.transaction do 26 tags = split_tag_names.map { |posts_tag| Tag.find_or_create_by!(posts_tag: posts_tag) } 27 post.update!(title: title, text: text, name: name) 28 end 29 rescue ActiveRecord::RecordInvalid 30 false 31 end 32 33 def to_model 34 post 35 end 36 37 private 38 39 attr_reader :post 40 def default_attributes 41 { 42 title: post.title, 43 text: post.text, 44 name: post.name.pluck(:name).join(',') 45 } 46 end 47 48 def split_tag_names 49 tag_names.split(',') 50 end 51end

viwes/edit

ruby

1<div class="contents row"> 2 <div class="container"> 3 <h3>編集する</h3> 4 <%= form_with model: @poststsg, url: "/posts/#{@post.id}",method:"put", local: true do |form| %> 5 <%= form.text_field :title, placeholder: "Nickname" %> 6 <%= form.text_field :name, placeholder: "tag" %> 7 <%= form.text_area :text, placeholder: "text", rows: "10" %> 8 <%= form.submit "SEND" %> 9 <% end %> 10 </div> 11</div>

README

1## users テーブル 2 3| Column | Type | Options | 4| ------------------ | ------ | ----------- | 5| nickname | string | null: false | 6| email | string | null: false | 7| encrypted_password | string | null: false | 8 9### Association 10 11- has_many :posts 12- has_many :comments 13 14## tags テーブル 15 16| Column | Type | Options | 17| ------ | ------ | ----------- | 18| name | string | null: false | 19 20### Association 21 22- has_many :post_tag_relations 23- has_many :posts, through: :post_tag_relations 24 25## post_tag_relations テーブル 26 27| Column | Type | Options | 28| ------ | ---------- | ------------------------------ | 29| post | references | null: false, foreign_key: true | 30| tag | references | null: false, foreign_key: true | 31 32### Association 33 34- belongs_to :post 35- belongs_to :tag 36 37## comments テーブル 38 39| Column | Type | Options | 40| ------- | ---------- | ------------------------------ | 41| text | text | null: false | 42| user | references | null: false, foreign_key: true | 43| post | references | null: false, foreign_key: true | 44 45### Association 46 47- belongs_to :user 48- belongs_to :post 49 50## posts テーブル 51 52| Column | Type | Options | 53| ------- | ---------- | ------------------------------ | 54| title | string | null: false | 55| text | text | null: false | 56| user | references | null: false, foreign_key: true | 57 58### Association 59 60- has_many :post_tag_relations 61- has_many :tags, through: :post_tag_relations 62- has_many :comment 63- belongs_to :user

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

form_with model: @poststsg,
typo ですね a と s は隣同士だかr

投稿2021/07/21 15:23

winterboum

総合スコア23376

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

validate

2021/07/23 08:05

ありがとうございました
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問