2012-09-06 21 views
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

回答

0

如果u需要编辑后,再加入

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使用路由文件将url和请求(如GET/POST/PUT/DELETE)映射到控制器操作。

这里

PUT /posts/:id(.:format)      posts#update 

请求被PUT,控制器PostsController和动作是更新。

也,

post GET /posts/:id(.:format)      posts#show 

ü需要从表单通过http请求如果POST/PUT/DELETE。 u能为“编辑”

<%= form_for([@post,@comment],:url=>post_comment_path, :method => :put) do |f| %> 

映射到展会请求PostsController这样做。格式为html bydefault。有关更多信息,请详细了解服务器日志。

+0

我想编辑评论。 – hguser

+0

在评论控制器中添加相同的编辑/更新方法。 –