我理解不应该采用深层嵌套资源,但我不知道如何将其正确转换为1级嵌套资源。如果存在干净的教程,请向我展示教程,我将学习它。如果没有那么请帮我解决我的问题。这是我的route.rb
resources :article do
resources :picture
resources :comments do
resources :repcomments
end
end
resources :reports
我可以做文章,图片,评论但不确定如何处理第三级报告。我们的想法是,文章可以有评论,评论可以在哪里报告给管理层。 Everythings在文章#show pages和here
中播放 def show
@art = Article.find(params[:id])
@comments = @art.comments.find(:all, :order => 'created_at DESC')
respond_to do |format|
format.html # show.html.erb
format.json { render json: @art }
end
end
这里是文章#show中的视图表单
<%= @art.title %>
<%= @art.content %>
Comment
<%= form_for [@art, current_customer.comments.new] do |f| %>
<%= f.text_area :description %><br />
<%= f.submit "Add Comment" %>
<% end %>
# End of form comments
<% @comments.each do |comment| %>
<%= comment.description %>
<div>Report</div>
<%= form_for [@art, comment, repcomments.new] do |f| %>
<%= f.text_area :body %><br />
<%= f.submit "Report it" %>
<% end %>
<% end %>
我得到一个没有定义方法而不确定如何处理它。提前致谢
这里是路线文件
article_comment_repcomments GET /articles/:article_id/comments/:comment_id/repcomments(.:format) repcomments#index
POST /articles/:article_id/comments/:comment_id/repcomments(.:format) repcomments#create
new_article_comment_repcomment GET /articles/:article_id/comments/:comment_id/repcomments/new(.:format) repcomments#new
edit_article_comment_repcomment GET /articles/:article_id/comments/:comment_id/repcomments/:id/edit(.:format) repcomments#edit
article_comment_repcomment GET /articles/:article_id/comments/:comment_id/repcomments/:id(.:format) repcomments#show
PUT /articles/:article_id/comments/:comment_id/repcomments/:id(.:format) repcomments#update
DELETE /articles/:article_id/comments/:comment_id/repcomments/:id(.:format) repcomments#destroy
这里的模型
class Repcomments < ActiveRecord::Base
belongs_to :customer
belongs_to :article
attr_accessible :acknowledged, :body, :completed, :customer_id, :article_id
end
这里我的控制器重新命名
class RepcommentsController < ApplicationController
# GET /repcomments
# GET /repcomments.json
def index
@article = Article.find(params[:article_id])
@comments = @articles.comments.find(params[:comment_id])
@repcomments = Repcomment.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @repcomments }
end
end
# GET /repcomments/1
# GET /repcomments/1.json
def show
@article = Article.find(params[:article_id])
@comments = @articles.comments.find(params[:comment_id])
@repcomment = Repcomment.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @repcomment }
end
end
# GET /repcomments/new
# GET /repcomments/new.json
def new
@article = Article.find(params[:article_id])
@comments = @articles.comments.find(params[:comment_id])
@repcomment = Repcomment.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @repcomment }
end
end
# GET /repcomments/1/edit
def edit
@article = Article.find(params[:article_id])
@comments = @articles.comments.find(params[:comment_id])
@repcomment = @comments.repcomments.find(params[:id])
end
# POST /repcomments
# POST /repcomments.json
def create
@article = Article.find(params[:article_id])
@comments = @articles.comments.find(params[:comment_id])
@repcomment = @comments.repcomments.build(params[:repcomment])
respond_to do |format|
if @repcomment.save
format.html { redirect_to @repcomment, notice: 'Repcomment was successfully created.' }
format.json { render json: @repcomment, status: :created, location: @repcomment }
else
format.html { render action: "new" }
format.json { render json: @repcomment.errors, status: :unprocessable_entity }
end
end
end
# PUT /repcomments/1
# PUT /repcomments/1.json
def update
@article = Article.find(params[:article_id])
@comments = @articles.comments.find(params[:comment_id])
@repcomment = @comment.repcomments.find(params[:id])
respond_to do |format|
if @repcomment.update_attributes(params[:repcomment])
format.html { redirect_to @repcomment, notice: 'Repcomment was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @repcomment.errors, status: :unprocessable_entity }
end
end
end
# DELETE /repcomments/1
# DELETE /repcomments/1.json
def destroy
@article = Article.find(params[:article_id])
@comments = @articles.comments.find(params[:comment_id])
@repcomment = @comments.repcomments.find(params[:id])
@repcomment.destroy
respond_to do |format|
format.html { redirect_to repcomments_url }
format.json { head :no_content }
end
end
end
NameError in Article#show
Showing /home/jean/rail/voix/app/views/articles/show.html.erb where line #70 raised:
undefined local variable or method `repcomments' for #<#<Class:0xaf24860>:0xaafe934>
引用
几乎就是这样答案 0 :(得分:4)
只需查看您的代码,我就可以在您的路线中看到一个问题::article
和:picture
需要是复数。
resources :articles do
resources :pictures
resources :comments do
resources :repcomments
end
end
resources :reports
此外,您无需在:repcomments
内嵌套:articles
。除了路线中已有的内容,您还可以添加:
resources :comments do
resources :repcomments #and remove the other "resources :repcomments"
end
是的,您将resources :comments
定义两次,但没关系。
如果您从未对文章范围之外的评论做任何事情,那么您可以编写您的路线:
resources :comments, only: [] do
resources :repcomments
end
这样Rails就不会生成默认的休息路由(没有/评论,没有/评论/:id等)