1

我正在开发一个允许用户评论单个“作品”的应用程序(想想博客文章)。模型中的关联如下:

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 展示页面上有一个表单,允许用户发表评论:

<%= 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

最后,这里是 Comments 控制器:

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 控制器上找不到创建操作?任何帮助将不胜感激。

谢谢!

4

1 回答 1

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 中进行预编译。

于 2013-04-19T01:11:37.017 回答