代码之家  ›  专栏  ›  技术社区  ›  sofarsophie

在Rails 5中的文章中添加注释(深度嵌套资源)

  •  2
  • sofarsophie  · 技术社区  · 7 年前

    我有一个Rails应用程序,它有三个主要模型: Qn、Ans和评论 嵌套得很深 (注释嵌套较浅),它们都显示在 ,这让人非常困惑。

    http://localhost:3000/questions/2 ,用户可以看到所有 @问题.答案 答案 ,用户可以看到 答案.评论 答复 ,用户也可以提交新评论。

    undefined method `model_name' for {:url=>"/questions/4/answers/2/comments/new"}:Hash
    

    所以我试着通过params @可评论的 答复 特定控制器和动作 等等,但这些方法都不起作用。我猜我的控制器有问题,但我似乎不知道是什么。

    路线。rb(顶部)

    # Resources
    resources :sessions
    resources :users
    resources :bookmarks # to be implemented later
    
    resources :questions do
      resources :answers do
        resources :comments, shallow: true
      end
    end
    

    class Question < ApplicationRecord    
      has_many :answers
      has_many :bookmarks #later
    end
    

    class Answer < ApplicationRecord
      belongs_to :question
      has_many :comments, as: :commentable
    
      has_many :likes, as: :likeable
      validates :answercontent, length: {minimum: 50}
    end
    

    class Comment < ApplicationRecord
      belongs_to :commentable, polymorphic: true
    end
    

    <% @question.answers.each do |answer| %>
    // ommited
    <!-- Comments -->
    <% answer.comments.each do |comment| %>
       <%= comment.content %>
       <br>
    <% end %>
    
    <!-- Submit new comment -->
    <%= form_for(url: new_question_answer_comment_path, comment: {answer_id: answer.id, question_id: @question.id}) do |f| %>
       <%= f.text_area :content %>
       <%= f.submit "Submit" %>
    <% end %>
    <% end %>
    

    问题控制器(新建、创建、销毁,为简洁起见)

    class QuestionsController < ApplicationController
    def index
        @questions = Question.all
    end
    
    def show
        @question = Question.find(params[:id])
        @answers = Answer.all
    
        # Delete only appears when num_ans is 0
        @deletable = (current_user== User.find(@question.user_id)) && (@question.answers.all.size==0)
    
    end
    
    private
      def question_params
        params.require(:question).permit(:picture_url, :country, :educational_level, :topic)
      end
    
    end
    

    答案控制器(编辑、更新、销毁或简化)

    class AnswersController < ApplicationController
    
    def create
        @question = Question.find(params[:question_id])
        @answer = @question.answers.create(answer_params)
        @answer.question_id = @question.id
        @answer.user_id = current_user.id
    
        if @answer.save
            redirect_to @question
        else
            render :new
        end
    end
    
    private
      def answer_params
        params.require(:answer).permit(:user_id, :question_id, :answercontent)
      end    
    end
    

    class CommentsController < ApplicationController
    before_filter: load_commentable
    
    def index
      @commentable = Answer.find(params[:answer_id])
      @comments = @commentable.comments
    end
    
    def new
       @comment = @commentable.comments.new
    end
    
    def create
      @comment = @commentable.comments.new(params[:comment])
        if @comment.save
         redirect_to @commentable
      else
        render :new
      end
    end
    
    # From RailsCast ep.154
    private
    def load_commentable
      resource, id = request.path.split('/')[1,2]
      @commentable = resource.singularize.classify.constantize.find(id)
    end   
    end
    

    路线

     question_answer_comments GET    /questions/:question_id/answers/:answer_id/comments(.:format)     comments#index
                            POST   /questions/:question_id/answers/:answer_id/comments(.:format)     comments#create
    new_question_answer_comment GET    /questions/:question_id/answers/:answer_id/comments/new(.:format) comments#new
               edit_comment GET    /comments/:id/edit(.:format)                                      comments#edit
                    comment GET    /comments/:id(.:format)                                           comments#show
                            PATCH  /comments/:id(.:format)                                           comments#update
                            PUT    /comments/:id(.:format)                                           comments#update
                            DELETE /comments/:id(.:format)                                           comments#destroy
    

    提前感谢您的帮助。

    为了向您提供有关我尝试的解决方案的更多信息:

    <%= form_for([answer, @comment], url: new_question_answer_comment_path(answer.id, @question.id)) do |f| %>
    

    给了我:

    First argument in form cannot contain nil or be empty
    
    1. 使用@commentable(这基本上就是答案)会给我一个错误,即“id在@commentable中”。id不存在,因为@commentable为nil”。

    所以我认为问题是 @可评论的

    1 回复  |  直到 7 年前
        1
  •  1
  •   Pavan Cesar    7 年前

    form_for 期望 记录 第一个参数 注释实例 new_question_answer_comment_path 期望值 question_id answer_id 钥匙 创建新注释 路线 应该是 question_answer_comments new_question_answer_comment 所以你的 应该是

    <%= form_for Comment.new,url: question_answer_comments_path(@question,answer) do |f| %>
      <%= f.text_area :content %>
      <%= f.submit "Submit" %>
    <% end %>
    

    或者只是

    <%= form_for [Comment.new,@question,answer] do |f| %>
      <%= f.text_area :content %>
      <%= f.submit "Submit" %>
    <% end %>