1

深くネストされたリソースを採用すべきではないことは理解していますが、それを 1 レベルのネストされたリソースに適切に変換する方法がわかりません。クリーンなチュートリアルが存在する場合は、チュートリアルを見せてください。学習します。そうでない場合は、私の問題を解決してください。ここに私のroute.rb

resources :article do
  resources :picture
  resources :comments do
    resources :repcomments
  end
end
resources :reports

記事、写真、コメントはできますが、レポートの第 3 レベルにアプローチする方法がわかりません。記事にはコメントを付けることができ、コメントを管理者に報告できる場所が考えられます。すべてが article#show ページで再生され、ここではコントローラー

  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

article#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

ここに私のコントローラのrecomments

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>

そのため、ほとんどが参照です

4

1 に答える 1

4

あなたのコードを見るだけで、ルートに 1 つの問題があることがわかります。: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 :comments2 回定義したことになりますが、それで問題ありません。

記事の範囲外のコメントで何もしない場合は、次のようにルートを記述できます。

resources :comments, only: [] do
    resources :repcomments
end

この方法では、Rails はデフォルトの残りのルートを生成しません (/comments や /comments/:id などはありません)。

于 2012-08-24T15:46:09.213 に答える