Home>

The hierarchical category function is created with rails.
Functionally, there is no problem (category2 is displayed after selecting category1), but an error occurs when the post button is pressed, and the post is not saved.

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


The first time I encountered an error, I couldn't resolve it even after checking it, and I was stuck.
When you check your terminal,

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


Since the category id is properly taken like this, I don't know what this error indicates.
I think that the place where I feel a little uncomfortable is the processing of @post given to the post form.

Association is a little complicated and may be wrong at that point
Please give me a professor.

Code
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</pre>
<pre><code>resources: category1s, only: [] do
    resources: category2s, only:: index do
      resources: category3s, only:: index
    end
  end
belongs_to: category1
  belongs_to: category2
  belongs_to: category3
has_many: category2s,->{order (: id)}
belongs_to: category1, optional: true
  has_many: category3s,->{order (: id)}
belongs_to: category2, optional: true
  • Answer # 1

    The error says that there is nocategory1_idin the Post model.
    In the published model class

    t.string "category1"
    t.string "category2"
    t.string "category3"


    It is notcategory1_id. Isn't there typo?
    category1andcategory1_idare mixed even in other published code, but is it mixed?

Related articles

Trends