我正在开发一款允许用户评论单个“工作”的应用(想想博客文章)。模型中的关联如下:
class User < ActiveRecord::Base
has_many :works
has_many :comments
class Work < ActiveRecord::Base
belongs_to :user
has_many :comments
class Comment < ActiveRecord::Base
belongs_to :user
belongs_to :post
belongs_to :work
Works show页面上有一个表单,允许用户发表评论:
<%= form_for(@comment) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_area :content, placeholder: "Post a comment!" %>
</div>
<%= f.submit "Post", class: "btn btn-small btn-primary" %>
<% end %>
Works控制器如下。请注意,我在这里添加了构建注释功能,以便Works页面上的表单起作用:
class WorksController < ApplicationController
#before_filter :current_user, only: [:edit, :update]
def index
@works = Work.all
@comment = @work.comments.build(params[:comment])
@comment.user = current_user
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @works }
end
end
def create
@work = current_user.works.create(params[:work])
redirect_to current_user
end
def edit
@work = current_user.works.find(params[:id])
end
def new
@work = current_user.works.new
end
def destroy
@work = current_user.works.find(params[:id]).destroy
flash[:success] = "Work deleted"
redirect_to current_user
end
def update
@work = current_user.works.find(params[:id])
if @work.update_attributes(params[:work])
flash[:success] = "Profile updated"
redirect_to @work
else
render 'edit'
end
end
def show
@work = Work.find(params[:id])
@comment = @work.comments.build
@comment.user = current_user
@activities = PublicActivity::Activity.order("created_at DESC").where(trackable_type: "Work", trackable_id: @work).all
@comments = @work.comments.order("created_at DESC").where(work_id: @work ).all
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @work }
end
end
end
最后,这是评论控制器:
class CommentsController < ApplicationController
before_filter :authenticate_user!
def index
@comments = Comment.all
end
def show
@comment = Comment.find(params[:id])
@activities = PublicActivity::Activity.order("created_at DESC").where(trackable_type: "Comment", trackable_id: @comment).all
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @comment }
end
def update
@comment = current_user.comments.find(params[:id])
if @comment.update_attributes(params[:comment])
flash[:success] = "Comment updated"
redirect_to @comment
end
end
def create
@work = Work.find(params[:id])
@comment = @work.comments.build(params[:comment])
@comment.user = current_user
if @comment.save
#flash[:success] = "Post created!"
redirect_to @work
else
render 'home#index'
end
end
end
end
当我尝试使用作品展示视图页面上的评论表单提交评论时,我收到以下错误:
未知行动 无法为CommentsController
找到“创建”操作为什么不能在Comments控制器上找到创建操作?任何帮助将不胜感激。
谢谢!
答案 0 :(得分:2)
您的评论控制器show
操作缺少end
,即:
def show
@comment = Comment.find(params[:id])
@activities = PublicActivity::Activity.order("created_at DESC").where(trackable_type: "Comment", trackable_id: @comment).all
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @comment }
end
应该是这样的:
def show
@comment = Comment.find(params[:id])
@activities = PublicActivity::Activity.order("created_at DESC").where(trackable_type: "Comment", trackable_id: @comment).all
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @comment }
end
end
(现在它正在“落入”创建动作中,因此永远不会设置创建动作。)
然后您可能需要更改最终版本:
end
end
end
end
是
end
end
end
因为这可能会“补偿”不匹配并允许页面在Ruby中进行预编译。