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

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

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

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

Q&A

解決済

1回答

1518閲覧

[Rails] unknown attribute 'category1_id' for Post というエラーが発生する。

yupapapa

総合スコア24

Ruby on Rails

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

0グッド

0クリップ

投稿2019/12/05 13:05

編集2019/12/05 13:20

railsで階層カテゴリー機能を作成しています。
機能的には問題なく動作(category1を選択後,category2が表示される)するのですが、投稿ボタンを押したタイミングでエラーが発生し、投稿が保存されません。

発生しているエラー

ActiveModel::UnknownAttributeError in PostsController#create unknown attribute 'category1_id' for Post.

初めて遭遇したエラーで、調べてみてもなかなか解決ができず行き詰まっています。
ターミナルを確認すると、

"category1_id"=>"1", "category2_id"=>"1", "category3_id"=>"1"

このようにカテゴリーのidはきちんと取れている? ので、このエラーが何を示しているかがわかりません。
自分で少し違和感がある場所は、投稿フォームに与えてる、 @post の処理がこれであっているのかなと思っています。

関連付けも少しだけ複雑でそこの時点で間違えているかもしれません
どうかご教授願います。

コード

create_table "category1s", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.string "name" t.datetime "created_at", null: false t.datetime "updated_at", null: false end create_table "category2s", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.string "name" t.bigint "category1_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["category1_id"], name: "index_category2s_on_category1_id" end create_table "category3s", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.string "name" t.bigint "category2_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["category2_id"], name: "index_category3s_on_category2_id" end create_table "posts", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.string "name" t.text "content" t.integer "price" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "image" t.string "status" t.integer "user_id" t.string "category1" t.string "category2" t.string "category3" end
<% category1_options = Category1.order(:id).map { |c| [c.name, c.id, data: { children_path: category1_category2s_path(c) }] } %> <%= f.select :category1_id, category1_options, { prompt: "---" }, class: 'select-parent select-default' %> <% category2s = @post.category1.try(:category2s) || [] %> <% category2_options = category2s.map { |c| [c.name, c.id, data: { path: category1_category2_category3s_path(c) }] } %> <%= f.select :category2_id, category2_options, { prompt: "---" }, class: 'select-children select-default', style: "display: none;" %> <% category3s = @post.category2.try(:category3s) || [] %> <% category3_options = category3s.map { |c| [c.name, c.id] } %> <%= f.select :category3_id, category3_options, { prompt: "---" }, class: 'select-grandchildren select-default', style: "display: none;" %>
class PostsController < ApplicationController before_action :set_post, only:[:show] before_action :authenticate_user, only:[:new] def index end def show @post = Post.find(params[:id]) end def new @post = Post.new end def create @post = Post.new(post_params) if @post.save redirect_to root_url else render :new end end private def post_params params.require(:post).permit(:name,:content,:price,:image,:status, :category1_id, :category2_id, :category3_id).merge(user_id: @current_user.id) end def set_post @post = Post.find(params[:id]) end end
resources :category1s, only: [] do resources :category2s, only: :index do resources :category3s, only: :index end end

postrb

1 belongs_to :category1 2 belongs_to :category2 3 belongs_to :category3

category1rb

1 has_many :category2s, -> { order(:id) }

category2rb

1 belongs_to :category1, optional: true 2 has_many :category3s, -> { order(:id) }

category3rb

1 belongs_to :category2, optional: true

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

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

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

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

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

guest

回答1

0

ベストアンサー

エラーはPostモデルにはcategory1_idなんて無いよと言ってます。
掲載されたモデルクラスでは

ruby

1t.string "category1" 2t.string "category2" 3t.string "category3"

となっていてcategory1_idではありません。そこはtypoしてませんか?
他に掲載されているコード中でもcategory1category1_idが混在してるのですが、そこはあえて混在させているのでしょうか?

投稿2019/12/05 13:57

編集2019/12/05 14:00
NCC1701

総合スコア1680

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問