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 - creating rails controller
- ruby on rails - about rails db: seed when creating rails api in docker environment
- ruby on rails - regarding how to link categories and posts
- ruby - how to get "id" before creating "rails" record
- ruby on rails - i want to perform asynchronous communication with jquery on rails
- ruby on rails - to update the blank db in the non-enterable select box
- ruby on rails - unable to delete unnecessary migration files
- ruby on rails - rails arbitrarily sorts the prefix with a word that does not exist, resulting in an error that there is no corre
- ruby on rails - hash comes out when displaying radio button enum with each array
- ruby on rails - associate users with posts with rails
- ruby on rails - i want to open a list of individual details on the top page
- ruby on rails 6 - rails heroku can't resolve the debrow error "no method error: undefined method` + 'for nil: nilclass &quo
- ruby on rails - specifying the redirect destination does not work
- ruby on rails - why does the column not appear in the select box of view?
- ruby on rails - "undefined method` id 'for nil: nilclass "error occurs when users/show action (view my page) is execut
- ruby on rails - i would like to know how to get the values of the internally joined table
- about web application login (ruby on rails)
- ruby - rails does not show flash
- ruby on rails - the profile image registered in the users table is reflected on the screen only under each view file of users
- ruby - when using rails engine with rails4, i want to refer to the image used in js of parent application from the image file un
- ruby - i want to handle exceptions using rails rescue
- ruby - a version of rails that programming novices should learn from now
- gem rename does not work [ruby]
- ruby - [rails] implementation of search function using form_tag
- html - if @post = postnew(content: paramas[:content]), there is one column, so i want to increase it to a few
- login function cannot be implemented well with ruby on rails
- ruby - i'm getting an uninitialized constant applicationcontroller::standerderror and i don't know what code is causing the prob
- i want to make a new branch with rubymine
- ruby - i want to implement the ranking function