When posting a new post, I want to be able to select a category and post.

Please write your question in detail here.

Currently, I am creating a web application like twitter.
First, I implemented it so that it can be described in a temporary blank form.
However, I would like to be able to select the category using active_hash, and please explain the error during implementation.

By this error bundle install'active_hash' to the gem and
Run rails g model cotegory --skip-migration and
I implemented the category.

I was getting an error in a table that I thought I didn't need while creating a new post view.

NoMethodError in ItemsController # new
undefined method `data ='for Category (Table doesn't exist): Class Extracted source (around line # 2): 1 2 3 Four Five 6

class Category

Rails.root: /Users/uraokayuutarou/projects/NF

Application Trace | Framework Trace | Full Trace app/models/category.rb: 2: in' app/models/category.rb: 1: in

' app/models/item.rb: 5: in' app/models/item.rb: 1: in
' app/controllers/items_controller.rb: 10: in `new'

Corresponding source code
Ruby on rails

  <% = form_with (model: @item, local: true) do | form |%>
    <% = form.file_field: image%>
<% = Form.text_area: text, placeholder: "text", rows: "10"%>
    <% = form.text_field: category_id, placeholder: "cotegory"%>
    <% # Error when changing to this code%>
<% #<% = f.collection_select (: category_id, Category.all, {class: "select-box"})%>
    <% = form.text_field: item_id, placeholder: "nostalgia"%>
    <% = form.submit "SEND"%>
  <% end%>

class Item
What I tried

rails g model category --skip-migration
Instead, I created a table with rails g model category and executed it.
I was getting the same error and thought that the cause was that the code in new.html.erb was not written properly.

  • Answer # 1

    The category is not active_hash
    class Category It has become.