Home>

The following error message occurred while implementing the function to post a comment associated with the detail page while transitioning to the detail page using routing nesting.

Routing Error
No route matches [POST] "/ prototypes/5"
Corresponding source code
#Routing nesting
resources: prototypes do
  resources: comments, only :: create
end
#Processing with controller
def create
  if @comment = Comment.create (comment_params)
    @prototype = Prototype.find (params [: id])
    @comment = Comment.new
    redirect_to: show
  else else
    render: show
  end
end
private
def comment_params
    params.require (: comment) .permit (: text) .merge (user_id: current_user.id, protorype_id: params [: protorype_id])
end
<!-Comment input form associated with the transitioned page->
<% = form_with (mocdl: [@prototype, @comment], local: true) do | f |%>
  
  <% = f.label: text, "comment"%><br />
  <% = f.text_field: text%>
  
  
  <% = f.submit "submit", class :: form__btn%>
  
<% end%>
What I tried

The URL pattern that should have been sent
/ prototypes /: prototype_id/comments (.: format)
Confirm that the URL pattern sent by the form_with you wrote is
/ prototypes/5
So I understand that the cause is likely to be form_with, but I feel that the syntax is correct.
I also tried restarting the server, but it didn't work.

Supplementary information (FW/tool version, etc.)

ruby on rails v6.0.0

  • Answer # 1

    Before correction

    <% = form_with (mocdl: [@prototype, @comment], local: true) do | f |%>
    Revised
    <% = form_with (model: [@prototype, @comment], local: true) do | f |%>
    Fixes

    The model was mistaken for mocdl