Ruby on rails 使注释(在rails指南示例中)可编辑
我正在按照rails 3指南学习rails 博客应用程序现在已经运行,但是我想让评论可以编辑,并在post show页面中更新、创建表单。因此,我做了以下修改: post.show.htm.erb:Ruby on rails 使注释(在rails指南示例中)可编辑,ruby-on-rails,ruby-on-rails-3,routes,Ruby On Rails,Ruby On Rails 3,Routes,我正在按照rails 3指南学习rails 博客应用程序现在已经运行,但是我想让评论可以编辑,并在post show页面中更新、创建表单。因此,我做了以下修改: post.show.htm.erb: <h2>Comments</h2> <% @post.comments.each do |comment| %> <tr> <td><%= comment.commenter %></td> &l
<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 %>
我得到一个错误:
No route matches {:action=>"show", :controller=>"comments"}
事实上,正如你所看到的,我在CommentController中有表演动作
而且,我想知道为什么它会访问评论#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
。为什么评论#显示?如果需要编辑文章,请添加
def edit
@post = Post.find(params[:id])
end
def update
@post = Post.find(params[:id])
if @post.update_attributes(params[:post])
redirect_to some_path and return
end
render 'edit' #error
end
编辑表单应向服务器发送PUT请求。rails使用routes文件将url和请求(如GET/POST/PUT/DELETE)映射到控制器操作
这里
请求被放置,控制器为PostsController,操作为update
而且
如果表单是POST/PUT/DELETE,则需要从表单中传递http请求。
你可以为“编辑”这样做
<%= form_for([@post,@comment],:url=>post_comment_path, :method => :put) do |f| %>
post|u comment_path,:method=>:put)do | f |%>
映射到PostsController的显示请求。默认情况下,格式为html。有关更多信息,请详细查看服务器日志
def edit
@post = Post.find(params[:id])
end
def update
@post = Post.find(params[:id])
if @post.update_attributes(params[:post])
redirect_to some_path and return
end
render 'edit' #error
end
PUT /posts/:id(.:format) posts#update
post GET /posts/:id(.:format) posts#show
<%= form_for([@post,@comment],:url=>post_comment_path, :method => :put) do |f| %>