在show.html.erb
<%= form_for :comment, :url=> {:controller => 'comments', :action => 'create'} do |f| %>
<%= f.text_field :title %>
<%= f.text_area :comment %>
<%= f.hidden_field :id , :value => @post.id %>
<%= f.submit %>
<% end %>
在Comments_controller中
class CommentsController < ApplicationController
def create
@post = Post.find(params[:id])
@comments = @post.comments.create(params[:comment])
if @comments.save
redirect_to @post
else
redirect_to post_path
end
end
在Routes.rb 资源中:posts
match '/create', :to => 'comments#create' , :as => :create
当我从视图表单中添加任何评论时,它会给出以下错误:
'Couldn't find Post without an ID'
我不知道为什么 params[:id] 不返回 Post ID ?注意:我正在使用acts_as_commentable