FBa*_*z51 0 ruby ruby-on-rails
我试图按照以下指南启动Ruby on Rails.一切顺利,直到我试图销毁(删除评论).
我收到以下错误:找不到CommentsController的动作'show'
我将在下面发布我的代码.
http://guides.rubyonrails.org/getting_started.html
comments_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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
的routes.rb
Rails.application.routes.draw do
resources :articles do
resources :comments
end
root 'welcome#index'
end
Run Code Online (Sandbox Code Playgroud)
article.rb
class Article < ActiveRecord::Base
has_many :comments, dependent: :destroy
validates :title, presence: true,
length: { minimum: 5}
end
Run Code Online (Sandbox Code Playgroud)
comment.rb
class Comment < ActiveRecord::Base
belongs_to :article
end
Run Code Online (Sandbox Code Playgroud)
文章/ 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>
Run Code Online (Sandbox Code Playgroud)
只是不确定它是什么.请指教.如果需要,我会提供更多代码.
答案将在您的routes.rb和您尝试调用的URL中.
URL可能是/comment/123,并且routes.rb可能具有类似get 'comment/:id' => 'comment#show'的内容(或resource注释语句).
因此rails尝试显示页面以进行注释,但您没有实现该功能.错误消息正在说明它需要说什么(你的确没有.show方法CommentsController).
如果我的猜测不正确,请发布您的routes.rb和您要求的网址.
| 归档时间: |
|
| 查看次数: |
7211 次 |
| 最近记录: |