'Incomprehensible "The page you were looking for doesn't exist."
I am adding a comment function for each note. The specific method is to add a comment after entering the show of each note, and it will appear immediately below the same web page, as shown in the following code:
<h1>Note name<%= @note.title %></h1>
<%= @note.content %>
<%= form_with(model: @comment, url: note_comments_path(@note)) do |f| %>
<%= f.text_area :content %>
<%= f.submit "Add comment" %>
<% end %>
<ul>
<% @comments.each do |c| %>
<li>
<%= c.content %>
</li>
<% end %>
</ul>
However, when I press the "Add comment" button, the browser is redirected to the "The page you were looking for doesn't exist." error message page. I can't find anything missing from the controller. Please tell me what am I missing?
This is part of NotesController
class NotesController < ApplicationController
before_action :find_user_note, only: [:show, :edit, :update, :destroy]
before_action :check_login!, except: [:index, :show]
def index
@notes = current_user.notes.includes(:user).order(id: :desc)
end
def new
@note = current_user.notes.new
end
def create
@note = current_user.notes.new(note_params)
if @note.save
redirect_to "/notes"
else
redirect_to "/notes/new"
end
end
def show
@note = Note.find(params[:id])
@comment = @note.comments.new
@comments = @note.comments.order(id: :desc)
end
......
private
def find_user_note
@note = current_user.notes.find(params[:id])
end
def note_params
params.require(:note).permit(:title, :content)
end
end
This is CommentsController
class CommentsController < ApplicationController
before_action :check_login!
before_action :find_user_note
def create
@note.comments.new(comment_params)
if @note.save
@content = comment_params[:content]
else
redirect_to "/"
end
end
private
def comment_params
params.require(:comments).permit(:content).merge(user: current_user)
end
def find_user_note
@note = current_user.notes.find(params[:id])
end
end
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
