Home>
I'm trying to select a category, create content, and leave a comment on that content.
The routing is as follows.
resources: categories, only: [: index,: new,: create] do
resources: contents do
resources: comments
end
end
Up to the content could be saved with the following description
def create
@category = Category.find (params [: category_id])
@content = @ category.contents.new (content_params)
@ content.save
end
private
def content_params
params.require (: content) .permit (: content,: category_id) .merge (user_id: current_user.id)
end
What I tried
Comment input form
<% = form_with model: [@category, @content, @comment], class: "form", local: true do | f |%>
<form>
<% = f.text_field: comment, placeholder:'type a comment', class:'input-comment'%>
<% = f.submit'SEND', class:'send-btn'%>
</form>
<% end%>
Comment controller
def create
@category = Category.find (params [: category_id])
@content = Content.find (params [: content_id])
@comment = @ content.comments.new (comment_params)
@ comment.save
end
private
def comment_params
params.require (: comment) .permit (: comment,: content_id) .merge (user_id: current_user.id)
end
Parameters that came to the comment create method
Parameters: {"authenticity_token" =>"d1cssjlsFeCpwmnmzQeDkI3x49j/liIviVIVjeSr2LU8CtCXPz/mfZeFs5skbW8w + gDlYseQ/sV7Il8Ta7oTyg ==", "comment" == "," comment "=" category_id "=>" 1 "," content_id "=>" 1 "}
Error message
NoMethodError in CommentsController # create
undefined method `comments' for #<Content:
There seems to be a problem with @ comment = @ content.comments.new (comment_params) in the above method.
I tried various descriptions, but it doesn't work.
I am creating an application from scratch for the first time.
I think there are many unsightly parts, but please give me guidance.
Postscript -----
Association
devise: database_authenticatable,: registerable,
: recoverable,: rememberable,: validatable
has_many: contents, through:: comments
has_many: categories, through:: contents
has_many: comments
belongs_to: user
has_many: contents
has_many: users, through:: contents
belongs_to: user
belongs_to: category
has_many: user, through:: comments
belongs_to: user
belongs_to: content
Has become
-
Answer # 1
Related articles
- ruby on rails - not saved by create action
- ruby on rails - i don't understand the description of the form object pattern
- ruby on rails 5 - some items are not saved in db when posting from rails form
- ruby on rails - cannot save nested child model
- ruby - not saved with rails new save method
- ruby on rails - date_select cannot be saved
- ruby on rails - it is not saved in the database after registering the product
- ruby on rails - about the description in applicationjs
- ruby on rails - rails associated model is not saved in db
- ruby on rails - data cannot be saved in the intermediate table (parameters cannot be passed)
- ruby on rails 6 - raila6 nested routing
- ruby on rails - s3 images are not saved on the ec2 server
- ruby on rails - image cannot be saved (activestorage installed) null in image of table
- ruby on rails - i implemented it so that images can be saved in s3 of aws, but the view can no longer be displayed
- about description of manifest file of ruby on rails
- ruby on rails - how to retrieve id in controller with nested routing
- ruby on rails - description of rails packagejson
- ruby on rails - the image cannot be saved in s3 and cannot be displayed in the view aws :: sigv4 :: errors :: missingcredentials
- ruby on rails - questions about routes
Trends
- python - you may need to restart the kernel to use updated packages error
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- javascript - how to check if an element exists in puppeteer
- dart - flutter: the instance member'stars' can't be accessed in an initializer error
- sh - 'apt-get' is not recognized as an internal or external command, operable program or batch file
- i want to call a child component method from a parent in vuejs
- python 3x - typeerror: 'method' object is not subscriptable
Resolved by changing the association