我有一个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为零。但是我也在循环和控制器中指定了它。那么我还有什么可以尝试的?
表单上的路由不应该是'question_answer_comments_path'而不是'new_question_answer_comments_path'? –
@KarthikRavichandran要创建一条新评论,那就是实际使用的路线。 – Pavan
@KarthikRavichandran你说得对,其实我的错误 – Pavan