2012-12-25 65 views
0

我有用户模型和评论模型使用acts_as_commentable_with_threading
我试图把评论功能放在每个用户的显示页面部分。
但是当我尝试提交表单时,它显示路由错误。
这是为什么?为什么我的表单不能正确提交?

错误

Routing Error 
No route matches [POST] "/user/john/add_comment" 

型号/ user.rb

acts_as_commentable 

的意见/用户/ show.html.erb

<%= render 'comment', :user => @user %> 

的意见/用户/ _comment.html.erb

<table> 
    <tr> 
    <th>ID</th> 
    <th>Title</th> 
    <th>Body</th> 
    <th>Subject</th> 
    <th>Posted by</th> 
    <th>Delete</th> 
    </tr> 

<% @user.comment_threads.each do |comment| %> 
    <tr> 
    <td><%= comment.id %></td> 
    <td><%= comment.title %></td> 
    <td><%= comment.body %></td> 
    <td><%= comment.subject %></td> 
    <td><%= comment.user.user_profile.nickname if comment.user.user_profile %></td> 
    <td><%= button_to 'destroy', comment, confirm: 'Are you sure?', :disable_with => 'deleting...', method: :delete %></td> 
    </tr> 
<% end %> 
</table> 


<%= form_for @user, :html => { :class => 'form-horizontal' } do |f| %> 

    <div class="field"> 
     <%= f.label :body %><br /> 
     <%= f.text_field :body %> 
    </div> 

    <div class="actions"> 
    <%= f.submit %> 
    </div> 

<% end %> 

的routes.rb

get "user/:id/add_comment" => 'users#add_comment', :as => :add_comment_user 
get "user/:id/delete_comment" => 'users#delete_comment', :as => :delete_comment_user 

users_controller.rb

def add_comment 
    @user = User.find_by_username(params[:id]) 
    @user_who_commented = current_user 
    @comment = Comment.build_from(@user, @user_who_commented.id, params[:users][:body]) 
    @comment.save 
    redirect_to :controller => 'users', :action => 'show', :id => params[:users][:id] 
    flash[:notice] = "comment added!" 
end 
+0

经过我有确切的错误。 –

+0

@DaveNewton我刚刚加入 – HUSTEN

回答

1

因为你的路由是一个GET,并且表单是POST。

+0

我改变了我的路线,就像这样get **“user /:id/add_comment”=>'users#add_comment',:as =>:add_comment_user,:via =>:post **但它仍然说同样的错误:( – HUSTEN

+0

@HUSTEN为什么你会继续使用“get”? –

+0

哪里?我试图使用后 – HUSTEN

0

它需要是一条后路。你也发布到用户/:name/add_comment,你应该发布用户ID。您可以使用该名称,但这需要在所有用户中都是唯一的。

这条线也是错的。

@user = User.find_by_username(params[:id]) 

您可以在params[:username]find_by_id

+0

对不起,我使用to_param as slug这就是为什么 – HUSTEN

相关问题