我有一个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
-
使用@commentable(这基本上就是答案)会给我一个错误,即“id在@commentable中”。id不存在,因为@commentable为nil”。
所以我认为问题是
或
@可评论的