I don't know: how to add a category when creating a new Post.
Premise: There are Post model and Catgeory model. The Post model has title, content, and category_id columns, and is associated with the Category model.
<What I don't know specifically>
The following form was created using form_for.
Set category_id with a numeric value in the field "Category" in the image of the above form
<% = form_for (@post) do | f |%> <% = f.label: title%> <% = f.text_field: title%> <% = f.label: content%> <% = f.text_field: content, size: 50%> <% = f.label: category%> <% = f.number_field: category%> <% = f.submit "Submit"%> <% end%>
Then move to the controller.
class PostsController<ApplicationController def new @ post = Post.new end def create category = Category.find (category_params) @ post = category.posts.build (post_params) if @ post.save render 'normal/home' else redirect_to root_url end end private def category_params params.require (: post) .permit (: category_id) end def post_params params.require (: post) .permit (: title,: content) end end
At this time, category_id is not entered for some reason. It is tripped on the first line of create.
category = Category.find (2)
When it was, it went well.
I would be happy to tell you why the value of category_id is not included!
I did not understand even after examining.
I would appreciate your reply.
Answer # 1
I'm sorry, there were too few explanations.
category = Category.find_by (id: category_params)
category = Category.find_by (category_params)
How about doing this?
The parameter is a hash value, not just a value.
The contents are hoge: huga.
So, if you put a hash value in find that takes only a numeric value as an argument, will you get an error?
If you are wrong, I'm sorry.
category = Category.find (category_params [: category_id])
Is n’t this too?
Answer # 2
How about find_by?
- ruby on rails - while creating an ec site with rails, an error nomethoderror in cartscontroller#add_item appears when trying to
- ruby on rails - i want to realize multi-level categories
- ruby on rails - when you want to add more columns after generating a migration file with rails and adding columns
- ruby on rails - cannot rake rake task
- ruby - rails accepts_nested_attributes_for doesn't work
- ruby on rails - nomethoderror: undefined method `each' for 5:integer
- ruby on rails - i don't know the granularity of the commit
- ruby - [rails] i want to change the table to reflect using conditional branching
- ruby on rails - save the item selected in the select box of rails form_with
- ruby on rails 5 - first argument in form cannot contain nil or be empty appears with the rails cart function
- ruby on rails - rails6 like feature asynchronous communication missing required keys: [:id])
- ruby - i want to get out of the screen of yay! you're on rails!
- ruby on rails - activerecord::recordnotfound in taskscontroller#index error cannot be resolved
- ruby on rails - regarding the relationship of user_id in the seed file
- ruby on rails - how to change the appearance and tags dynamically with rails
- ruby - [rails] when registering new data in form_with, nomethoderror appears and the process fails
- ruby on rails 5 - an error occurs if there is a form in form_with
- ruby - what to do with the rails group task management app db association
- ruby - about classes and modules in rails
- ruby - when i deploy the rails app, i get an application error
- ruby - in the rails folder, the chrome driver that was working stopped working
- ruby - [rails] i want to add multiple records for each date and reflect them
- html - when i try to introduce the follow function, i am stuck with rake db:migrate and cannot get out
- i want to eliminate validation due to belongs_to has_many in ruby on rails
- ruby - unable to save data in books table the id becomes nil
- ruby - i'm getting an error with the google authentication implementation of characters
- ruby - △△ how to write haml
- ruby - render function is not reflected well haml usage
- ruby - rails can't transition to "new post" or "login" screen
- i want to add a browsing function with ruby on rails