【发布时间】:2023-03-04 17:40:01
【问题描述】:
我正在尝试按照以下指南来启动 Ruby on Rails。一切都很顺利,直到我尝试销毁(删除 cmets)。
我收到以下错误: 找不到 CommentsController 的操作“显示”
我将在下面发布我的代码。
http://guides.rubyonrails.org/getting_started.html
cmets_controller.rb
class CommentsController < ApplicationController
http_basic_authenticate_with name: "dhh", password: "secret", only: :destroy
def create
@article = Article.find(params[:article_id])
@comment = @article.comments.create(comment_params)
redirect_to article_path(@article)
end
def destroy
@article = Article.find(params[:article_id])
@comment = @article.comments.find(params[:id])
@comment.destroy
redirect_to article_path(@article)
end
private
def comment_params
params.require(:comment).permit(:commenter, :body)
end
end
articles_controller
class ArticlesController < ApplicationController
http_basic_authenticate_with name: "dhh", password: "secret", except: [:index, :show]
def index
@articles = Article.all
end
def show
@article = Article.find(params[:id])
end
def new
@article = Article.new
end
def edit
@article = Article.find(params[:id])
end
def create
@article = Article.new(article_params)
if @article.save
redirect_to @article
else
render 'new'
end
end
def update
@article = Article.find(params[:id])
if @article.update(article_params)
redirect_to @article
else
render 'edit'
end
end
def destroy
@article = Article.find(params[:id])
@article.destroy
redirect_to articles_path
end
private
def article_params
params.require(:article).permit(:title, :text)
end
end
routes.rb
Rails.application.routes.draw do
resources :articles do
resources :comments
end
root 'welcome#index'
end
article.rb
class Article < ActiveRecord::Base
has_many :comments, dependent: :destroy
validates :title, presence: true,
length: { minimum: 5}
end
comment.rb
class Comment < ActiveRecord::Base
belongs_to :article
end
articles/index.html.erb
<h1>Listing Articles</h1>
<%= link_to 'New article', new_article_path %>
<table>
<tr>
<th>Title</th>
<th>Text</th>
<th colspan="3"></th>
</tr>
<% @articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.text %></td>
<td><%= link_to 'Show', article_path(article) %></td>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<td><%= link_to 'Destroy', article_path(article),
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
只是不确定它会是什么。请指教。如果需要,我会提供更多代码。
【问题讨论】:
-
你能不能把你的评论和文章模型过去
-
刚刚发布了模型先生。谢谢!
-
请发布出现错误的 URL。
-
localhost:3000/articles/2/cmets/1 是给我消息的链接。
-
为什么我得到-1?无论如何..仍在努力!感谢任何帮助!
标签: ruby-on-rails ruby