Hello, it is necessary that my Comment model form_for [@account, @account.comments.build] account_id that will receive from form_for [@account, @account.comments.build] i.e. from a specific account, and user_id which will receive from current_user.id (devise).

Tell me what link to put and how to set up the form and the controller so that the Comment object user_id 2 parameters: user_id and account_id . Account is a product, and the user is a model for registering users (buyers). And each logged in user needs to add the ability to comment on the account (product).

  • Why is the link polymorphic? Do you have comments will be used elsewhere, in addition to commenting on your account? - cheops
  • @cheops I want to display the commentator email using the data of the device account that was logged in during the creation of the comment, and account_id is needed to correctly display the comments for each account. - Escobar
  • Out of idle curiosity: how are your account and user connected? What is the reason for their separation? - D-side
  • @ D-side account and user are not connected with me yet. Account is a product, and the user is a model for registering users (buyers). And each logged in user needs to add the ability to comment on the account (product). - Escobar
  • @Escobar add this to the question. - D-side

1 answer 1

You do not need it.

In the form you can not specify these fields at all.

user_id is not needed there because the server at the time of creation can install it itself from current_user , its presence is not needed in the form of a comment. Consider: what will happen if the user through DevTools (or analog) user_id markup and specifies a different value in user_id ? Or do you intend to additionally check the user for honesty with an additional code? Isn't it easier to just not ask the browser a question if you know the answer in advance?

When you create a comment, overwrite current_user.comments , and the corresponding field in the created comment will appear automatically.

 @comments.merge(current_user.comments).create(comment_params) # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^ # | | # наложение strong parameters 

account_id ... depends. It may or may not be needed. In an amicable way, it should not, but it requires a little detail ... which you seem to have already implemented?

Judging by your form_for , you have a comments resource nested in accounts :

 resources :accounts do resources :comments end 

... then the comment creation URL of yours takes the form /accounts/{N}/comments . So, you also know the account on which the comment is left without explicit indication directly in the form (this information is recorded in the action-attribute of the form, the address to which the form makes the request).

And in the end, the algorithm for creating a new comment will be something like this:

 @account = Account.find(params[:account_id]) @account. comments. merge(current_user.comments). create(comment_params) 

... add error handling and authorization to taste.

... and what you are doing in new is completely irrelevant. Though Comment.new . This is only important if you want to hide some fields in the form.

  • yes implemented. Thanks again) - Escobar
  • @ Escobar did you figure it out? If so, it would be nice to see a daw on the answer, they say, "the explanation is exhaustive, the problem is solved." Thank! - D-side
  • Yes, thanks, I figured it out, sorry, I forgot to put it. - Escobar