Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 同一观点中的评论、问题和答案_Ruby On Rails_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 同一观点中的评论、问题和答案

Ruby on rails 同一观点中的评论、问题和答案,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,我试图添加问题和答案的评论与形式都在评论显示页面。添加问题有效,但添加答案会重定向到“/questions//answers”,并出现错误 No route matches [POST] "/questions/answers" 以下是我的路线: resources :comments do resources :questions do end end resources :questions do resources :answers do end end 以下是评论显

我试图添加问题和答案的评论与形式都在评论显示页面。添加问题有效,但添加答案会重定向到“/questions//answers”,并出现错误

No route matches [POST] "/questions/answers"
以下是我的路线:

resources :comments do
  resources :questions do
 end
end 

resources :questions do
  resources :answers do
 end
end 
以下是评论显示视图:

<% @questions.each do |question| %>
<%= @comment.question %>
<%= render :partial => "answers/form" %>
<% end %>
<% @questions.each do |question| %>
    <%= @comment.question %>
    <%= render :partial => "answers/form", locals: {question:question} %>
<% end %>
最后是控制器的答案:

def show
@comment = Comment.find params[:id]
@questions = @comment.questions
@answer = Answer.new      
end
def create
@question = Question.find(params[:question_id])
@answer = @question.answers.new(answer_params)
end

谢谢

你更有可能找到这样的工作:

questions/1/answers
只是猜测而已。您可以检查
rake routes
的输出,我可以想象您将看到如下内容:

questions/:id/answers

在create方法中,您永远不会保存到数据库中
@answer
永远不会有id

应答器控制器#创建

不是

我看不出你在分享的代码中会出现错误,但即使不是修复。。。这将是一个问题。

在您的答案表中

<%= simple_form_for [@question, Answer.new] do |f| %>
    <%= f.input :body %>   
<% end %>
@answer = @question.answers.new(answer_params)
<%= simple_form_for [@question, Answer.new] do |f| %>
    <%= f.input :body %>   
<% end %>
<% @questions.each do |question| %>
    <%= @comment.question %>
    <%= render :partial => "answers/form", locals: {question:question} %>
<% end %>
<%= simple_form_for [question, Answer.new] do |f| %>
      <%= f.input :body %>   
<% end %>
def create
    @question = Question.find(params[:question_id])
    @answer = @question.answers.new(answer_params)
    @answer.save
end