当前位置 : 主页 > 编程语言 > ruby >

ruby-on-rails – 文章中的NoMethodError#show.undefined method` article_comments_path’

来源:互联网 收集:自由互联 发布时间:2021-06-23
**ARTICLE CONTROLLER** //controllerclass ArticlesController ApplicationControllerdef index@article=Article.allenddef show@article=Article.find(params[:id])@comment = Comment.new @comment.article_id = @article.id enddef new@article=Article.n
**ARTICLE CONTROLLER**                  //controller
class ArticlesController < ApplicationController
	def index
	@article=Article.all
	end
	def show
	@article=Article.find(params[:id])	
	@comment = Comment.new
    @comment.article_id = @article.id 	
	end
	def new
	@article=Article.new
	end
	def create
	@article = Article.new(article_params)
    @article.save
    redirect_to article_path(@article)
	end
	def destroy
	article=Article.find(params[:id])
    article.destroy
    redirect_to articles_path
	end
	def edit
	 @article = Article.find(params[:id])	
	end
	def update
	@article = Article.find(params[:id])
    @article.update(article_params)
    flash.notice = "Article '#{@article.title}' Updated!"
    redirect_to article_path(@article)
	end
	def article_params
    params.require(:article).permit(:title, :body)
    end
end

**SHOW.HTML.ERB**            /view file

<h1><%=@article.title%></h1>
<p><%=@article.body%></p>
<br><hr>

<%= link_to "edit", edit_article_path(@article) %>|
<%= link_to "delete",article_path(@article), method: :delete,data: {confirm: "Really delete the article?"} %>|
<%= link_to "<< Back to Articles List", articles_path %>
<h3>Comments</h3>
<%= render partial: 'articles/comment', collection: @article.comments %>
<%= render partial: 'comments/form' %>

**_FORM.HTML.ERB**             //_form view
<h3>Post a Comment</h3>

<%= form_for [ @article, @comment ] do |f| %>
  <p>
    <%= f.label :author_name %><br/>
    <%= f.text_field :author_name %>
  </p>
  <p>
    <%= f.label :body %><br/>
    <%= f.text_area :body %>
  </p>
  <p>
    <%= f.submit 'Submit' %>
  </p>
<% end %>
   rake routes:
 Prefix Verb   URI Pattern                  Controller#Action
articles GET    /articles(.:format)          articles#index
         POST   /articles(.:format)          articles#create
 new_article GET    /articles/new(.:format)      articles#new
edit_article GET    /articles/:id/edit(.:format) articles#edit
 article GET    /articles/:id(.:format)      articles#show
         PATCH  /articles/:id(.:format)      articles#update
         PUT    /articles/:id(.:format)      articles#update
         DELETE /articles/:id(.:format)      articles#destroy
    root GET    /                            articles#index
comments GET    /comments(.:format)          comments#index
         POST   /comments(.:format)          comments#create
 new_comment GET    /comments/new(.:format)      comments#new
edit_comment GET    /comments/:id/edit(.:format) comments#edit
 comment GET    /comments/:id(.:format)      comments#show
         PATCH  /comments/:id(.:format)      comments#update
         PUT    /comments/:id(.:format)      comments#update
         DELETE /comments/:id(.:format)      comments#destroy

我没有找到articles_comments_path!
文章中的NoMethodError#show
显示/home/manoj/ror/blogger/app/views/comments/_form.html.erb,其中第3行引发:

未定义的方法article_comments_path’for#<#< Class:0x00000005be3d40>:0x000000054cacc0>
应用程序/视图/评论/ _form.html.erb:3:in_app_views_comments__form_html_erb__3077497298558231225_47236640′
app / views / articles / show.html.erb:11:在`_app_views_articles_show_html_erb__4529829459036724249_48053660′

您收到此错误是因为您没有article_comments_path帮助程序方法.将您的路线更改为:

resources :articles do
  resources :comments
end

要么

如果您不想嵌套路由,请将表单更改为:

<%= form_for @comment do |f| %>
  // form fields
<% end %>

同样在show action中,您可以使用association methods来清理代码

def show
  @article= Article.find(params[:id])   
  @comment = @article.comments.build 
end
网友评论