2013-04-24 100 views
3

我的应用工作正常直到我决定为评论添加编辑功能。在此之前,我可以查看特定的文章,但不是现在。当我尝试访问具有一些评论的特定文章(文章/节目)时,现在我得到路由错误No route matches {:action=>"edit", :controller=>"comments", :article_id=>nil, :id=>nil}。 其实我试图为文章实现评论编辑功能。为此,我在注释/ _comment.html.erb中加入了“编辑”链接。注意,只有当文章有评论并且文章没有评论时,我才会得到这个错误。 我对文章和评论有不同的看法。没有路线匹配{:action =>“edit”,:controller =>“comments”,:article_id => nil,:id => nil}编辑ruby on rails博客文章评论

comments_controller.rb

class CommentsController < ApplicationController 
     before_filter :user_signed_in, except: [:create] 
     def new 
      @comment = Comment.new 
     end 

     def create 
      @article = Article.find(params[:article_id]) 
      @comment = @article.comments.build(params[:comment]) 
      @comment.user_id = current_user.id 
      @comment.save 
      flash[:success] = "Comment created!" 
      redirect_to article_path(@comment.article) 
     end 

     def edit 
     @comment = Comment.find(params[:id]) 
     end 

     def update 
     @comment = Comment.find(params[:id]) 
     @article = @comment.article 
     respond_to do |format| 
      if @comment.update_attributes(params[:comment]) 
      redirect_to @article_path(@article) 
      else 
      render :action => "edit" 
      end 
     end 

     def destroy 
     @comment = Comment.find(params[:id]) 
     @article = Article.find(params[:article_id]) 
     @comment.destroy 
      redirect_to @article_path(@artilce) 
     end 

    end 

评论/ edit.html.erb

<h3>Editing comment</h3> 
    <%= render :partial => 'comment_form' %> 

评论/ _comment_form.html.erb

<%= form_for ([@article, @article.comments.build]), :required => true do |f| %> 
    <%= f.hidden_field :article_id %> 
    <%= f.text_area :content, :style => "width:727px; height:100px; border: 1px solid #999999;margin-top:80px; background-color:#FFFFFF;margin-left:-33px" %> 
     <div class="actions"> 
     <%= f.submit "Add Comment", :style => "margin-right:20px; margin-left:560px; background-color:#66C9Ef; color:#FFFFFF; border: 0px solid #82b548; border-radius: 3px 3px 3px 3px; font-size: 1.3rem;" %> 
     </div> 
    <% end %> 

评论/ comment.html.erb(在这里我已经给出了编辑评论文章的链接)

<% if @article.comments.count >= 1 %> 
     <div style="border: px solid #66c9ee;border-radius: 0px 0px 0px 0px;margin: 10px -30px 15px; padding:  10px 15px 25px; background: none repeat scroll 0 0 #F2F2F2; width:700px; font-size: 1.2em;border-bottom: 0px solid #DDDDDD;"> 
      <%= comment.content %> 
      <div id="tabula"> 
       <ul id="tabula"> 
       <li> <div style="color: #0077CC;margin-rigth:200px; font-size: 1.0em;margin-top:4px;background-color:#;"> <%= comment.user.username if comment.user %></div></li> 
       <li> <div style="color: #0077CC; background-color:; margin-top:4px; margin-left:25px;"> <p> <%= time_ago_in_words(comment.created_at.in_time_zone("Asia/Calcutta")) unless comment.created_at.nil? %> </p></div></li> 
       <li> <div style="color: #0077CC; background-color:; margin-top:4px; margin-left:25px;"> <%= link_to "edit", edit_article_comment_path(@article,comment) %> </div></li> 
       </ul> 
       </div> 
       </div> 
     <% else %> 
      <div style="color:#0077CC;margin-left:25px;font-size:1.4em;"> be first to comment</div> 
     <% end %> 

耙路线的结果。(不完整)

  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 
        PUT /articles/:id(.:format)       articles#update 
        DELETE /articles/:id(.:format)       articles#destroy 
    dashboard_index GET /dashboard(.:format)        dashboard#index 
        POST /dashboard(.:format)        dashboard#create 
     new_dashboard GET /dashboard/new(.:format)       dashboard#new 
     edit_dashboard GET /dashboard/:id/edit(.:format)      dashboard#edit 
      dashboard GET /dashboard/:id(.:format)       dashboard#show 
        PUT /dashboard/:id(.:format)       dashboard#update 
        DELETE /dashboard/:id(.:format)       dashboard#destroy 


        tags GET /tags(.:format)         tags#index 
         POST /tags(.:format)         tags#create 
       new_tag GET /tags/new(.:format)        tags#new 
       edit_tag GET /tags/:id/edit(.:format)       tags#edit 
        tag GET /tags/:id(.:format)        tags#show 
         PUT /tags/:id(.:format)        tags#update 
         DELETE /tags/:id(.:format)        tags#destroy 
     article_comments GET /articles/:article_id/comments(.:format)   comments#index 
         POST /articles/:article_id/comments(.:format)   comments#create 
    new_article_comment GET /articles/:article_id/comments/new(.:format)  comments#new 
    edit_article_comment GET /articles/:article_id/comments/:id/edit(.:format) comments#edit 
     article_comment GET /articles/:article_id/comments/:id(.:format)  comments#show 
         PUT /articles/:article_id/comments/:id(.:format)  comments#update 
         DELETE /articles/:article_id/comments/:id(.:format)  comments#destroy 
         GET /articles(.:format)        articles#index 
         POST /articles(.:format)        articles#create 
         GET /articles/new(.:format)       articles#new 
         GET /articles/:id/edit(.:format)      articles#edit 
         GET /articles/:id(.:format)       articles#show 
         PUT /articles/:id(.:format)       articles#update 
         DELETE /articles/:id(.:format)       articles#destroy 

的routes.rb

Mau::Application.routes.draw do 
     devise_for :users 
     root :to => 'articles#index' 
     resources :articles 
     resources :dashboard 
     resources :tags 
     resources :articles do 
     resources :comments 
    end 
    end 

comments_comment.html.erb

<% if @article.comments.count >= 1 %> 
    <div style="border: px solid #66c9ee;border-radius: 0px 0px 0px 0px;margin: 10px -30px 15px; padding:  10px 15px 25px; background: none repeat scroll 0 0 #F2F2F2; width:700px; font-size: 1.2em;border-bottom: 0px solid #DDDDDD;"> 
     <%= comment.content %> 
     <div id="tabula"> 
      <ul id="tabula"> 
      <li> <div style="color: #0077CC;margin-rigth:200px; font-size: 1.0em;margin-top:4px;background-color:#;"> <%= comment.user.username if comment.user %></div></li> 
      <li> <div style="color: #0077CC; background-color:; margin-top:4px; margin-left:25px;"> <p> <%= time_ago_in_words(comment.created_at.in_time_zone("Asia/Calcutta")) unless comment.created_at.nil? %> </p></div></li> 
      <li> <div style="color: #0077CC; background-color:; margin-top:4px; margin-left:25px;"> <%= link_to "edit", edit_article_comment_path(@article ,@comment) %> </div></li> 
      </ul> 
      </div> 
      </div> 
    <% else %> 
     <div style="color:#0077CC;margin-left:25px;font-size:1.4em;"> be first to comment</div> 
    <% end %> 
+0

你可以发布你的文章视图和_comment偏? – AlexBrand 2013-04-24 11:29:45

+0

嗨alexBrand,我已添加_comment partia。关于文章的观点,你是问文章/表演还是索引页面。我已经添加了文章/显示页面。如果您需要更多代码粘贴,请让我知道。 – 2013-04-24 11:39:49

+0

可以告诉我们你的耙路线结果吗?可能有错误的路线路径 – Nich 2013-04-24 12:00:44

回答

2
<% if @article.comments.count >= 1 %> 
    <div "..."> 
    <%= comment.content %> # <-- This. 

我把我的怀疑在<%= comment.content %>和其他comment变量发现于comments_comment.html.erbcomments/comment.html.erb

看起来似乎对我来说似乎合理,因为如果有问题的文章没有评论,您就不会收到错误,因为@article.comments.count >= 1返回false

为了有一个工作comment变量,if语句后插入一个循环:

<% if @article.comments.count >= 1 %> 
    <div "..."> 
    <% @article.comments.each do |comment| %> # <-- This. 
     <%= comment.content %> 
     ... 
    <% end %> # Remember to close the loop. 
    </div> 
<% else %> 
... 

此外,请注意在评论/ _comment.html.erb使用comment代替@commentedit_article_comment_path

+0

即使我觉得问题在于部分评论。但我完全不知道。我已经把“编辑”链接评论/ _comment.html.erb。这对你来说很好看。 – 2013-04-24 12:27:02

+0

hi Sunxperous,但是当编辑操作在comments_controller.rb中时,它如何显示路由错误。 – 2013-04-24 12:34:57

+1

我已经扩展了我的答案,看看它是否解决了路由错误问题。错误可能是由'edit_article_comment_path'造成的。 – Sun 2013-04-24 12:37:07

0

如果方法#count返回1或更大值,则只显示注释。当你调用方法计数时,它会直接在数据库上计数,如果文章没有任何评论,它将不会呈现任何内容,这是因为您可以看到没有评论的文章。

但是,当评论存在时,您正在构建一个新评论以显示在表单中,并尝试为其创建编辑链接。

所以我的解决方案是,改变你的form_for,因为你不需要重新发送你的所有评论,传递一个新的评论作为参数,而不是建立一个评论集合。然后在另一个视图上,您​​可以为每个持久评论创建一个编辑链接。

+0

嗨Paulo,感谢您的建议。是否需要更改form_for中的参数或是否需要更改form_for本身。您可以解释一下它。谢谢 – 2013-04-24 12:32:30

+1

只需更改form_for的这一行“<%= form_for([@article,Comment.new]),:required = > true do | f |%>“ – 2013-04-24 12:34:16

+1

嗨Paulo Henrique,我尝试了你的建议,但仍然出现错误。没有路由匹配{:action =>“edit”,:controller =>“comments”,:article_id =>#

,: ID =>零}。它显示id是零。这是造成的问题。 – 2013-04-24 12:38:58

相关问题