我正在开发一个允许用户评论单个“作品”的应用程序(想想博客文章)。模型中的关联如下:
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#create 中的 Activerecord::RecordNotFound
没有身份证找不到工作
为什么应用程序无法找到该作品以便将评论与其关联?
编辑1: 感谢下面的答案,我编辑了评论表:
<%= form_for(@work, @comment) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_area :content, placeholder: "Post feedback or contribute content
to this work!" %>
</div>
<%= f.submit "Post", class: "btn btn-small btn-primary" %>
<% end %>
更改表单并添加嵌套路由后,我仍然遇到相同的错误。
我编辑了路由文件以包含工作评论的嵌套:
authenticated :user do
root :to => 'activities#index'
end
root :to => "home#index"
devise_for :users
resources :users do
member do
get :following, :followers, :posts, :comments
end
end
resources :works do
resources :comments
end
resources :relationships, only: [:create, :destroy]
resources :posts
resources :activities
resources :comments
Rake 路由为 Comments#create 显示以下内容: POST /comments(.:format)
POST URL(出现错误的地方)是 appURL/works/1/comments
似乎不对。我需要改变什么?非常感谢您到目前为止的帮助!