0

私はacts_as_commentable_with_threadingを使用してユーザーモデルとコメントモデルを持っています
。各ユーザーのショーページにコメント機能を部分的に配置しようとしています。
しかし、フォームを送信しようとすると、ルーティング エラーが表示されます。
どうしてこれなの?

エラー

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

models/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 %>

ルート.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
4

2 に答える 2

1

ルートは GET 用であり、フォームは POST であるためです。

于 2012-12-25T13:33:39.217 に答える
0

郵便ルートである必要があります。また、user/:name/add_comment に投稿しています。ユーザー ID を投稿する必要があります。名前を使用できますが、これはすべてのユーザーで一意である必要があります。

この行も間違っています。

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

のいずれかを渡すことができparams[:username]ますfind_by_id

于 2012-12-25T13:53:11.243 に答える