1
<%= simple_form_for :addcomments, :url => addcomments_post_path, :multipart => true ,:method => 'post' do |f| %>
<%= f.input :title %>
<%=  f.input :body %>
<%= f.button :submit %>

这是我对帖子添加评论的部分视图,下面是我的帖子->显示视图

<div>
<h4>Comments </h4><br>
<%= @all_comments.each do |t| %>
  <h3><%= t.title %></h3> <br>
  <h4><%= t.body %></h4>   <br>
  <% end %>
  <%= render :partial => 'addcomment' %>
</div>

Comments_controller.rb 是

class CommentsController < ApplicationController

# before_filter :load_commentable


def addcomments
@post = Post.find(params[:id])
@user = current_user
@comment = Comment.build_from(@post,@user,params[:comment])
if @comment.save
  redirect_to post_url
else
  redirect_to post_url
end
end
end

路由.rb

resources :comments do
member  do
  post  'addcomments', to: 'comments#addcomments'
end
end

当我运行此代码并单击 post-> show 时,出现错误

The action 'addcomments' could not be found for CommentsController
4

1 回答 1

0

您正在使用多态模型,因此您的第一级路线应该是“帖子”而不是“评论”。

# resources :comments do # Change this line to
resources :posts do
  member do
    post 'addcomments', to: 'comments#addcomments'
  end
end
于 2013-05-11T03:23:55.590 回答