2013-04-24 114 views
0

我的应用工作正常,直到我决定添加评论的编辑功能。在此之前,我可以查看特定的文章,但不是现在。我在评论视图中添加了edit.html.erb页面,并且_comment_form.html.erb为partial.Now我无法访问文章/ 12(例如),当我尝试时,它说No route matches {:action=>"edit", :controller=>"comments", :article_id=>#<Article id: 21, title: "good for health", body: "uby on Rails 3 Model Data Validation\r\nRails Model D...", created_at: "2013-04-14 15:14:19", updated_at: "2013-04-24 05:51:20", user_id: 1, impressions_count: 7>, :id=>#<Comment id: nil, content: nil, user_id: nil, article_id: 21, created_at: nil, updated_at: nil>}编辑ruby on rails评论时的路由错误博客

基本上,我试图在文章/展示页面上实现评论编辑功能。为此,我在评论视图下添加了edit.html.erb。但现在没有任何工作。任何建议都会升值。

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 %> 

的routes.rb(未满)

 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 

注:当我访问了一篇文章不有意见,它的工作原理,但是当我尝试访问有评论的文章,​​它显示上面的路由错误。任何建议都会升值。

回答

0

我太低排置评,但 请发表您的路线文件,但我认为有可能是一个问题在这里/articles/:article_id/comments

您在编辑说明没有一个ID。这是一个新的变量吗?请在该方向

+0

喜活着,谢谢。我发布了我的routes.rb文件。请看看这个。我没有得到关于“/ articles /:article_id/comments”和你想改变的地方。请详细说明。 – 2013-04-24 08:38:18

+0

您发布的内容是'rake routes'的产物。我想'routes.rb'文件内容 – 2013-04-24 08:47:03

+0

对不起Alive。这里的routes.rb: - 茂:: Application.routes.draw做 devise_for:用户 根:到=> '文章#指数' 资源:文章 资源:仪表盘 资源:标签 资源:文章做 资源:评论 结束 结束 – 2013-04-24 09:19:57

0
  1. 尝试评论编辑链接调查和检查它是否工作.. 是否行得通,那么问题是在编辑注释方法。

  2. 你的路径应该是edit_article_comment_path(article_id的,COMMENT_ID),因为评论是嵌套控制器,它总是需要一个文章编号,以示对你的回应

+0

嗨TechGuy,你是对的,当我评论编辑链接,它的作品。但我需要实施编辑评论。任何建议。 – 2013-04-24 09:14:29

+0

您好TechGuy,根据您的第二个建议,我已将编辑路径更改为edit_article_comment_path(attherate article_id,atherate comment_id),但几乎没有变化。我使用了@ article_id和@ comment_id。但现在我得到了路由错误 没有路由匹配{:action =>“edit”,:controller =>“comments”,:article_id => nil,:id => nil} – 2013-04-24 09:27:48