Home>
  1. Number list

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

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.

ActionView :: SyntaxErrorInTemplate in ItemsController # new
Encountered a syntax error while rendering template: check    Post<% = 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 occurs when changing to this code%><% = f.collection_select (: category_id, Category.all: id,: name, {},

 {class: "category-select"})%><% = form.text_field: item_id, placeholder: "nostalgia"%><% = form.submit "SEND"%><% end%><% #<% = f.collection_select (: genre_id, Genre.all,: id,: name, {},

 {class: "genre-select"})%><% #<% = f.submit "submit", class: "btn"%>%>%>Extracted source (around line # 9):
    <% #<% = form.text_field: category_id, placeholder: "cotegory"%>%>  <% # Error when changing to this code%><% = F.collection_select (: category_id, Category.all: id,: name, {},

 {class: "category-select"})%>  <% = form.text_field: item_id, placeholder: "nostalgia"%>  <% = form.submit "SEND"%><% end%>
Corresponding source code
app/controllers/item_controller.rb
class ItemsController        Post   <% = 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%>  <% # Previous code below<% = f.collection_select (: category_id, Category.all, {class: "select-box"})%><% = F.collection_select (: category_id, Category.all: id,: name, {},

 {class: "category-select"})%>  <% = form.text_field: item_id, placeholder: "nostalgia"%>  <% = form.submit "SEND"%><% end%>app/models/category.rb
class Category
What I tried

I think there is a problem with the controller,
First, I tried to approve category_id for params.
private
def item_params
params.require (: item) .permit (: image,: text) .merge (user_id: current_user.id)
➡︎params.require (: item) .permit (: image,: text: category_id) .merge (user_id: current
_user.id)
➡︎ Syntax Error occurs with the above code
end

After that, because the view is not completed
<% = f.collection_select (: category_id, Category.all, {class: "select-box"})%>
Modified from the above code to: id,: name, {}, Add
% = f.collection_select (: category_id, Category.all: id,: name, {}, {class: "category-select"})%>

Supplementary information (FW/tool version, etc.)
  • Answer # 1

    <% #<% = form.text_field: category_id, placeholder: "cotegory"%>%>

    Is

    <% #<% = form.text_field: category_id, placeholder: "cotegory"%>


    Will be commented out,

    %>


    Only remains, so isn't this causing a SyntaxError?

Related articles