ruby-on-rails – 文章中的NoMethodError#show.undefined method
**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! 未定义的方法article_comments_path’for#<#< Class:0x00000005be3d40>:0x000000054cacc0> 解决方法
您收到此错误是因为您没有article_comments_path帮助程序方法.将您的路线更改为:
resources :articles do resources :comments end 要么 如果您不想嵌套路由,请将表单更改为: <%= form_for @comment do |f| %> // form fields <% end %> 同样在show action中,您可以使用 def show @article= Article.find(params[:id]) @comment = @article.comments.build end (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |