Home>
Status

I am making an app like Tabelog. There are three models, the User model, the Shop model, and the Review model, and we are creating a form where users can post reviews to the store.

Write in the review submission form from the shop details page and post.
For example/ shops/1/showSkip the information from to the create action of the reviews controller.

problem

When I submit the form, I get an error that the reviews controller on the receiving side has no parameters.
The errors displayed on the screen are as follows.

So when I checked the screen of the form with the developer tools, the form did not have the proper name attribute. Actuallyname = "review [title]"What I want you to be likename = "title"The information was not received in the proper form set in strongparams.

I think there is probably a problem with how to write form_with, but how can I fix it?
The form is on the shop details screen (/ shops/1) And from there/ users/1/reviews /Skip the POST action to (to associate the logged-in user with the post).
I'm a little unsure about how to write the routing ...

The form is as follows.

<% = form_with (model: @review, url: user_reviews_path (current_user), local: true) do | f |%>
 <% = f.label: title, "Title"%>
 <% = f.text_field: title, class:'form-control'%>
 <% = f.label: content, "body"%>(Please write within 300 characters)
 <% = f.text_area: content, rows: 10, class:'form-control text_area'%>
 <% = hidden_field_tag: shop_id, @ shop.id%>
 <% = f.submit'submit', class: "btn btn-dark"%>
<% end%>


Please let us know if you have any other information needed to resolve it.

  • Answer # 1

    ■ I'm a little unsure about how to write the routing
    Is the Shop model routing nested in the Review?

    resources: shops, only: [: new,: create] do
      resources: reviews, only: [: index,: create]


    If (the actions below only are examples),

    <% = form_with model: [@shop, @review], local: true do | f |%>


    As mentioned above, you can implement which store you want to save as a review in the first argument @shop of form_with, if you set an instance (Review.new) from the Review model in the second argument @review in advance. I think so.

  • Answer # 2

    It was because I didn't create an instance of review in Shops Controller.@review = @ shop.reviews.buildI was able to solve it by adding. Thank you for your help.

  • Answer # 3

    As a makeshift measurescopeIt is a good idea to set.

    <% = form_with (model: @review, scope:: review, url: user_reviews_path (current_user), local: true) do | f |%>

    It should be noted that@reviewWas set properlyReviewIf it's a class object

    <% = form_with (model: [current_user, @review], local: true) do | f |%>

    Should be possible.

    If you get an error, there is probably a problem with your controller or model.

  • Answer # 4

    Probably below, the action in the form tag will remain the same, with name = "review [title]".
    <% = form_with url: user_reviews_path (current_user), scope:: review do | f |%>
    If method becomes get,
    <% = form_with url: user_reviews_path (current_user), scope:: review, method:: post do | f |%>
    Please try.
    How is it?