0
我正在學習導軌遵循導軌3指南。使註釋(在導軌指南實例)可編輯
博客應用程序現在已經運行,但是我想讓這個評論可編輯,並在後期展示頁面中進行更新,創建表單。所以我提出以下modifiation:
post.show.htm.erb:
<h2>Comments</h2>
<% @post.comments.each do |comment| %>
<tr>
<td><%= comment.commenter %></td>
<td><%= comment.body %></td>
<td><%= link_to 'Edit', edit_post_comment_path(@post,comment) %></td>
<td><%= link_to 'Destroy', post_comment_path(@post,comment), confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
</table>
<h2>Add a comment:</h2>
#here,I can not set the form_for property.
<%= form_for([@post,@comment],:url=>post_comment_path) do |f| %>
<div class="field">
<%= f.label :commenter %><br />
<%= f.text_field :commenter %>
</div>
<div class="field">
<%= f.label :body %><br />
<%= f.text_area :body %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
控制器控制器:
class CommentsController < ApplicationController
# GET /posts/1/comments/1/edit
def edit
#render json: params
@post=Post.find(params[:post_id])
@comments=Comment.all
@comment = Comment.find(params[:id])
render "/posts/show"
end
#other action omitted
def show
# I donot know what to do here
end
end
但是我不能訪問該鏈接:
http://localhost:3000/posts/1
我得到錯誤:
No route matches {:action=>"show", :controller=>"comments"}
事實上,你可以看到我在CommentController中有show動作。
而且,我想知道它爲什麼會訪問註釋#show action?
這是我的路線:
post_comments GET /posts/:post_id/comments(.:format) comments#index
POST /posts/:post_id/comments(.:format) comments#create
new_post_comment GET /posts/:post_id/comments/new(.:format) comments#new
edit_post_comment GET /posts/:post_id/comments/:id/edit(.:format) comments#edit
post_comment GET /posts/:post_id/comments/:id(.:format) comments#show
PUT /posts/:post_id/comments/:id(.:format) comments#update
DELETE /posts/:post_id/comments/:id(.:format) comments#destroy
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format) posts#show
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
home_index GET /home/index(.:format) home#index
root / home#index
的/posts/:id
將觸發posts#show
。爲什麼comments#show
?
我想編輯評論。 – hguser
在評論控制器中添加相同的編輯/更新方法。 –