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.
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.
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
Related articles
- ruby on rails - unknown action error when trying to post a comment
- ruby on rails - test code causes error when product information input is successful
- ruby on rails - i get an error without being created
- ruby on rails - rails error (logged out users see links that are no longer valid)
- ruby on rails - pg :: datatypemismatch error when doing heroku run rails db: migrate
- ruby on rails - when i run rails, i get an error related to webpacker
- ruby on rails - i want to download a file with rails, s3 and carrierwave, but i get an error
- ruby on rails - error when implementing rails6 follow function
- ruby on rails 6 - i want to solve the error that occurred while implementing the product listing function
- ruby - an error occurs when building a rails 6 environment with docker
- ruby on rails 6 - about rails error "wrong number of arguments (given 1, expected 0)"
- ruby - when i try to update rails, i get a routing error
- ruby - [rails] about the error that occurs when deleting likes (routing error)
- ruby on rails 6 - when i press new user registration, i get an error of no route matches [get] "/ users"
- ruby on rails - about passing the attribute of ror class as an argument
- ruby on rails - rspec validation error
- ruby on rails 6 - [error] uninitialized constant user (call'userconnection' to establish a connection) :: image
- ruby on rails - i want to resolve rails name error
- ruby - [rails] the class attribute specified by the div tag enclosed in simple_format is not applied
- ruby on rails - nomethoderror in itemscontroller # new error
Trends
The error says that there is no
category1_id
in the Post model.In the published model class
It is not
category1_id
. Isn't there typo?category1
andcategory1_id
are mixed even in other published code, but is it mixed?