必要事項すべて入力し「投稿」ボタンを押すけど成功しない
解決したいこと
Ruby on RailsでWebアプリをつくっています。
記事を投稿する機能の実装中にエラーが発生しました。
投稿するボタンを押したとき正常にデータベースに登録されるようにしたいです。
発生している問題・エラー
form.text_field "virtues: []"
の時
app/views/posts/new.html.erb
<% @post.virtues = Array.new(10) if @post.virtues.blank? %> <!-- ここで virtues を明示的に10個の配列として初期化 -->
<% @post.virtues.each_with_index do |virtue, i| %>
<div class="mb-4">
<%= form.label "virtue_#{i + 1}", "美点 #{i + 1}", for: "post_virtue_#{i + 1}", class: 'label' %>
<%= form.text_field "virtues: []", value: virtue, id: "post_virtue_#{i + 1}", required: true, class: 'input input-bordered w-full' %>
</div>
<% end %>
上記newファイルの時のターミナルログが下記になります。
Cannot render console from 172.24.0.1! Allowed networks: 127.0.0.0/127.255.255.255, ::1
web-1 | 11:50:04 web.1 | Processing by PostsController#create as TURBO_STREAM
web-1 | 11:50:04 web.1 | Parameters: {"authenticity_token"=>"[FILTERED]", "post"=>{"name"=>"わーちゃん", "relationship"=>"私", "virtues["=>{"]"=>"j"}}, "commit"=>"投稿する"}
web-1 | 11:50:04 web.1 | User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
web-1 | 11:50:04 web.1 | Unpermitted parameter: :virtues[. Context: { controller: PostsController, action: create, request: #<ActionDispatch::Request:0x00007f09c81794d0>, params: {"authenticity_token"=>"[FILTERED]", "post"=>{"name"=>"わーちゃん", "relationship"=>"私", "virtues["=>{"]"=>"j"}}, "commit"=>"投稿する", "controller"=>"posts", "action"=>"create"} }
form.text_field "post[virtues][]"
の時
app/views/posts/new.html.erb
<% @post.virtues = Array.new(10) if @post.virtues.blank? %> <!-- ここで virtues を明示的に10個の配列として初期化 -->
<% @post.virtues.each_with_index do |virtue, i| %>
<div class="mb-4">
<%= form.label "virtue_#{i + 1}", "美点 #{i + 1}", for: "post_virtue_#{i + 1}", class: 'label' %>
<%= form.text_field "post[virtues][]", value: virtue, id: "post_virtue_#{i + 1}", required: true, class: 'input input-bordered w-full' %>
</div>
<% end %>
| Cannot render console from 172.24.0.1! Allowed networks: 127.0.0.0/127.255.255.255, ::1
web-1 | 13:26:59 web.1 | Processing by PostsController#create as TURBO_STREAM
web-1 | 13:26:59 web.1 | Parameters: {"authenticity_token"=>"[FILTERED]", "post"=>{"name"=>"わーちゃん", "relationship"=>"私", "post[virtues"=>[{"]"=>"a"}, {"]"=>"b"}, {"]"=>"c"}, {"]"=>"d"}, {"]"=>"e"}, {"]"=>"f"}, {"]"=>"g"}, {"]"=>"h"}, {"]"=>"i"}, {"]"=>"j"}]}, "commit"=>"投稿する"}
web-1 | 13:26:59 web.1 | User Load (1.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
web-1 | 13:26:59 web.1 | Unpermitted parameter: :post[virtues. Context: { controller: PostsController, action: create, request: #<ActionDispatch::Request:0x00007f09a1cd0288>, params: {"authenticity_token"=>"[FILTERED]", "post"=>{"name"=>"わーちゃん", "relationship"=>"私", "post[virtues"=>[{"]"=>"a"}, {"]"=>"b"}, {"]"=>"c"}, {"]"=>"d"}, {"]"=>"e"}, {"]"=>"f"}, {"]"=>"g"}, {"]"=>"h"}, {"]"=>"i"}, {"]"=>"j"}]}, "commit"=>"投稿する", "controller"=>"posts", "action"=>"create"} }
該当するソースコード
該当する箇所は上記app/views/posts/new.html.erb
と下記ファイルが関係していると思いました。
- パラメーターを設定したファイル
app/controllers/posts_controller.rb
class PostsController < ApplicationController
def index
@posts = Post.includes(:user)
end
def new
@post = Post.new
end
def create
@post = current_user.posts.build(post_params)
if @post.save
redirect_to posts_path, success: t('defaults.flash_message.created', item: Post.model_name.human)
else
flash.now[:danger] = t('defaults.flash_message.not_created', item: Post.model_name.human)
render :new, status: :unprocessable_entity
end
end
private
def post_params
params.require(:post).permit(:name, :relationship, virtues: [])
end
end
自分で試したこと
期待されるパラメーターの構造が下記になります
{
"post" => {
"name" => "わーちゃん",
"relationship" => "私",
"virtues" => ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
}
}
なので上記「発生しているエラー」で記入したようにform.text_field
の部分をいじってみましたが、すべて入力していますが投稿成功になりません。
ポストテーブルは現在以下のようになっています
create_table "posts", force: :cascade do |t|
t.bigint "user_id", null: false
t.string "name", null: false
t.integer "relationship", null: false
t.jsonb "virtues", default: [], null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["user_id"], name: "index_posts_on_user_id"
end
よろしくお願いいたします。
0