我正在开发一款允许用户评论单个“工作”的应用(想想博客文章)。模型中的关联如下:
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
当我尝试使用作品展示视图页面上的评论表单提交评论时,我收到以下错误:
在Activerecord :: RecordNotFound
找不到没有ID的作品
为什么应用程序找不到Work,以便它可以将注释关联到它?
编辑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路线显示以下注释#create: POST /comments(.:format)
POST URL(出现错误的地方)是appURL / works / 1 / comments
似乎不对。我需要改变什么?非常感谢你们的帮助!
答案 0 :(得分:3)
您的表单必须为form_for([@work, @comment])
,以便Rails知道如何构建/works/123/comments
之类的网址。现在只需发布到/comments
。
检查rake routes
以查看CommentsController #create动作的路线。您可能还需要调整控制器以阅读params[:work_id]
而不是params[:id]
。
答案 1 :(得分:1)
默认情况下,视图帮助器form_for(@comment)
将发布到“/ comments”。您可以指定包含工作记录:id
的网址(see the guides)。典型的方法是使用form_for([@work, @comment])
,只要您使用注释作为嵌套的工作资源设置路由,Rails就会为您执行此操作。