2013-04-24 48 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

相關問題