2012-09-06 4 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 %> 

컨트롤러 컨트롤러 :

http://localhost:3000/posts/1 
:

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 

그러나 내가 링크에 액세스 할 수 없습니다

오류가 발생합니다.

No route matches {:action=>"show", :controller=>"comments"} 

실제로 볼 수 있듯이, 나는 CommentController에서 show 액션을가집니다.

그리고 내가 왜 댓글 # 쇼 동작에 액세스 할까?

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/:idposts#show를 트리거 :

이 내 경로입니다. 왜 comments#show? u는 게시물을 수정해야하는 경우

답변

0

, 다음 편집 할 양식이 서버에 PUT 요청을 보내야합니다

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 

를 추가합니다. 레일스는 라우트 파일을 컨트롤러 동작에 사용하여 url과 요청 (GET/POST/PUT/DELETE와 같은)을 매핑합니다. 여기서 요청 넣어

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

는 제어기가 PostsController이고 동작은 업데이트된다. 그 POST/PUT는/삭제하면도

,

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

는 유 형태로 HTTP 요청을 전달해야한다. 은 u는 '편집'가 PostsController에 대한 공연 요청에

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

지도를 수행 할 수 있습니다. 형식은 html by default입니다. 자세한 정보는 서버 로그를 자세히보십시오.

+0

댓글을 수정하고 싶습니다. – hguser

+0

주석 컨트롤러에서 동일한 편집/업데이트 방법을 추가하십시오. –