我的網站能夠讓用戶成功發佈最高級別的評論,但我在評論兒童方面遇到了很多麻煩。我使用多態關聯,因此評論模型可以應用於文章,配置文件和圖片。在這三種情況下,評論都會將該模型視爲「可評論的」。螺紋評論不適用於我
我想在每個單獨的評論旁邊創建一個回覆鏈接,該回復鏈接將該特定評論的「id」傳遞到評論「新」視圖頁面中。然後我想要註釋「create」方法使用該id,@top_level_comment = Comment.find(id),並使用用戶在「新建」評論視圖中指定的參數創建一個新的孩子@ top_level_comment.children.create (params [:comment]),但我不知道如何將它寫入代碼。
因此,用戶將一篇文章顯示頁面,其中有下面的控制器上開始:
def show
@article = Article.find(params[:id])
@commentable = Article.find(params[:id])
@comments = @commentable.comments
@comment = Comment.new
@title = @article.title
end
然後,他會向下滾動到底部的意見,並看到有一個回覆鏈接通過文章顯示頁面:
<div id="comment <%= comment.id %>">
<%= comment.title %>
| <%= link_to "Permalink", polymorphic_path([@commentable, comment]), :action => :show %>
| <%= link_to "Reply", polymorphic_path([@commentable, @comment]), :action => :new %>
| <%= link_to "Edit Comment", polymorphic_path([@commentable, comment]), :action => :edit %>
| <%= link_to 'Delete Comment', [[@commentable, comment]], :confirm => "Are you sure?", :method => :delete %><br />
<%= comment.content %><br />
<%= comment.user.name %><br />
<%= @comment.children.count %><br /><br />
<%= render :partial => 'shared/comment', :collection => @comment.children %>
</div>
這是此行特別是當我有問題:
| <%= link_to「Reply」,polymorphic_path([@ commentable,@comment]),:action =>:new%>
如何更改此操作以從頂部的描述這個帖子?多態路徑不起作用。我認爲它不起作用,因爲commentable只適用於文章,配置文件和圖片,但不能評論。如何更改路徑,以便在傳遞當前評論的標識(comment.id)時轉到評論「新」頁面?
而且,我添加新的評論形式如下:
<%= form_for([@commentable, @comment]) do |f| %>
<%#= render 'shared/error_messages', :object => f.object %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :content %><br />
<%= f.text_area :content %>
</div>
<div class="actions">
<%= f.submit "Post Comment" %>
</div>
<% end %>
這裏是路由的樣子:
resources :articles do
resources :comments
end
您好,當我按照標記的路由,我得到這個時我嘗試查看文章:
No route matches {:controller=>"comments", :action=>"create", :article_id=>#<Article id: 300, title: "Filler Title", content: "Sunt sit est incidunt et.", user_id: 6, created_at: "2010-09-08 17:42:10", updated_at: "2010-09-08 17:42:10">}
添加以下新信息9月16日3:48 PST:
下面是允許用戶評論可評論內容的表單,但當@commentable是評論時它不起作用。
1: <%= form_for([@commentable, @comment]) do |f| %>
2: <%#= render 'shared/error_messages', :object => f.object %>
3: <div class="field">
4: <%= f.label :title %><br />
評論控制器「新」
def new
@commentable = find_commentable
@comment = Comment.new
end
當回覆評論,這是不言而喻以上,但它不知道@commentable是什麼,所以它的價值是零回覆到時評論。如何使@commentable成爲用戶按下回復的@comment?以下是允許用戶回覆評論的鏈接:
| <%= link_to "Reply", new_comment_path(comment.children.new) %>
非常感謝。
你好,當我使用以下內容:<(%)=的link_to 「回覆」,polymorphic_path([評論,comment.children.new]),:動作=>:新%> 然後我得到這個錯誤:未定義的方法'comment_comments_path'爲#<#:0xb1b9134>。我已經在主題中發佈了我的routes.rb。 –
Kelp
2010-09-14 22:08:34
嗨。您需要設置路線。我將編輯我的答案^還要刪除action =>:new,就像我的例子。 :) – mark 2010-09-14 22:19:44
你好,我添加了路線(減去地圖部分,我使用的是Rails 3),並且我收到一個錯誤信息,我將添加到主文章中。 – Kelp 2010-09-14 22:34:33