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.
/ shops/1/showSkip the information from to the create action of the reviews controller.
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. Actually
name = "review [title]"What I want you to be like
name = "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 measure
scopeIt 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 properly
ReviewIf 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 |%>
How is it?
- ruby - the last db setting doesn't work when deploying rails app on aws
- ruby - use form_with in rails to specify multiple models and save values
- ruby on rails 5 - the setup for everyday rails chapter 2 doesn't work (could not find shared context "project setup")
- ruby - rails render flash doesn't work
- ruby on rails - rails likes feature doesn't show up until you reload the page after clicking like
- ruby on rails undefinde method doesn't work
- ruby on rails 5 - rspec controller test doesn't work
- ruby on rails - rails doesn't work
- ruby on rails 5 - time calculation doesn't work
- ruby on rails - [rails] post form created with form_with does not work properly
- ruby on rails - action cable doesn't work on heroku
- ruby on rails - launching unicorn doesn't work
- ruby - rails: fattachment_field gives undefined method error
- ruby - nomethoderror in posts # new in form_with
- ruby on rails 5 - the path is changed by redirect_to of rails
- ruby - allow rails chat app to save to only one room
- ruby on rails - description of rails packagejson
- ruby on rails - i want to change from the state where only one posted image is displayed to the state where two posted images ar
- ruby - [rails] how to resolve activerecord errors
- ruby - passing variables using render partial
- ruby - i get an error with the rails new command
- [ruby on rails] i want to display a parent model that matches the information of the associated child model
- ruby - i want to solve the n + 1 problem when using rails where method in loop processing
- ruby - rails cannot be started in the production environment when building an ec2 environment
- ruby - if you try to make the value of the array (json) sent on the vue side a strong parameter, a nomethoderror error will occu
- ruby - i want to display the edit button and delete button on the index page
- ruby - i want to generate a random password (mixed alphanumeric characters)
- ruby - sass :: syntaxerror on rails s