2017-07-06 90 views
1

我有一個Rails應用程序,它有三個主要模型:Qn,Ans和Comments。我一直在做1層深的嵌套資源,但這三個資源是深嵌套(評論是淺層嵌套),他們都顯示在單一視圖,這使得它很混亂。在Rails 5中添加評論文章(深度嵌套資源)

在一個像這樣的網址:http://localhost:3000/questions/2,用戶可以看到所有使用循環顯示的問題:@ question.answers。在每個這些答案中,用戶可以看到使用循環顯示的answer.comments。在每個回答以下,用戶也可以提交新評論。

但嘗試多次實施1)循環顯示所有評論和2)形式的新評論後,我總是得到的線沿線的一些錯誤:

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

於是,我就在PARAMS通過@commentable而不是答案,或指向特定控制器和操作等,但這些方法都沒有工作。我猜測我的控制器有一個問題,但我似乎無法弄清楚。

routes.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 

Comment模型:

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

的show.html。ERB(的QuestionsController)

<% @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 %> 

QuestionsController(新,創建,銷燬中省略爲簡潔起見)

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 

AnswersController(編輯,更新,破壞中省略爲簡潔起見)

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 

感謝。

更新:

爲了讓你對我有什麼企圖解決方案的更多信息: 1.傳遞兩個參數,如:

<%= 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 
  • 使用@commentable(這基本上是答案)給了我一個錯誤,說'@ commentable.id中的id不存在@commentable沒有'。
  • 所以我覺得這個問題是答案@commentable爲零。但是我也在循環和控制器中指定了它。那麼我還有什麼可以嘗試的?

    +1

    表單上的路由不應該是'question_answer_comments_path'而不是'new_question_answer_comments_path'? –

    +0

    @KarthikRavichandran要創建一條新評論,那就是實際使用的路線。 – Pavan

    +0

    @KarthikRavichandran你說得對,其實我的錯誤 – Pavan

    回答

    1

    form_for預計記錄第一個參數,你的情況應該是評論實例。此外new_question_answer_comment_path預計值question_idanswer_id,因爲你是創建一個新的評論,則路線question_answer_commentsnew_question_answer_comment所以你form_for應該

    <%= 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 %> 
    
    +0

    是的,我注意到了,錯誤被修復了!現在我正在處理路由,當按下'submit'按鈕時,我被重定向到'http:// localhost:3000/questions/1/answers/1/comments',而不是原始問題頁面,錯誤。謝謝! – sofarsophie

    +0

    @SeHyunPark你可以在這裏粘貼錯誤日誌嗎? – Pavan

    +0

    我得到這個錯誤:失敗保護響應期間出現錯誤:/Users/sophiepark/Desktop/miaoforum/app/controllers/comments_controller.rb:2:語法錯誤,意外':',期待keyword_end before_filter:load_commentable //這是很奇怪,因爲當我10分鐘前試圖提交時,我得到了'POST'的錯誤路徑。 – sofarsophie