我对Ruby和Ruby on Rails很陌生。现在在我的应用程序中,我有一个微博,问题和答案控制器。对于路线,我有问题嵌套在微博下,并且问题的嵌套答案。在微博中我有一个问题部分和一个问题形成部分,并在问题部分我有一个答案部分和答案形成局部。特别是,错误是在答案表格部分触发的。
我的路线:
resources :microposts, only: [:show, :create, :destroy] do
resources :questions
end
resources :questions do
resources :answers
end
(微博)show.html.erb
<% provide(:title, @micropost.title) %>
<div class="center jumbotron">
<h2><%= link_to @micropost.user.name, @micropost.user %></h2>
<h1><%= @micropost.title %></h1>
<p><%= @micropost.content %>
<%= image_tag @micropost.picture.url if @micropost.picture? %>
</p>
<p>
Posted <%= time_ago_in_words(@micropost.created_at) %> ago.
<% if current_user?(@micropost.user) %>
<%= link_to "delete", @micropost, method: :delete,
data: { confirm: "Are you sure you want to delete this post?" } %>
<% end %>
</p>
<div class="row">
<% if @micropost.questions.any? %>
<ol class="questions">
<%= render @micropost.questions %>
</ol>
<% end %>
<% if current_user?(@micropost.user) %>
<%= render 'questions/question_form' %>
<% end %>
</div>
</div>
_question.html.erb
<li id="question-<%= @micropost.questions %>">
<span class="content"><%= question.qcontent %></span>
<% if question.answers.any? %>
<ol class="answers">
<%= render question.answers %>
</ol>
<% end %>
<% if logged_in? %>
<%= render 'answers/answer_form' %>
<% end %>
</li>
_answer_form.html.erb
<div class="col-md-6 col-md-offset-3">
<%= form_for([@question, new_question_answer_path]) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_area :acontent, placeholder: "Enter your answer" %></div>
<%= f.submit "Submit Answer", class: "btn btn-primary" %>
<% end %>
</div>
answers_controller.rb
class AnswersController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy]
def new
@question = Question.find(params[:question_id])
@answer = Answer.new
end
def create
@question = Question.find(params[:question_id])
@answer = @question.answers.build(answer_params)
redirect_to micropost_path(@micropost)
if @question.save
flash[:success] = "Answer submitted!"
end
end
def destroy
@question.destroy
flash[:success] = "Answer deleted"
end
private
def answer_params
params.require(:answer).permit(:acontent)
end
end
这是我在发布问题时加载微博页面时遇到的错误:
ActionView :: Template ::错误(没有路由匹配{:action =&gt;&#34; new&#34;,:controller =&gt;&#34;答案&#34;,:id =&gt;&# 34; 4&#34;}缺少必需的键:[:question_id]):
所以我认为我需要做的就是把它传递给question_id。但是,我该怎么做?
答案 0 :(得分:0)
对于您的路线,您希望嵌套像
resources :microposts, only: [:show, :create, :destroy] do
resources :questions do
resources :answers
end
end
这会得到你的网址
/microposts/:micropost_id/questions/:question_id/answers/answer_id
根据您的描述判断,这是您正在寻找的。这可以解决您的问题。或者让我知道我是否误解了你的问题。
然而,这不是一个好习惯,你永远不想深入嵌套多个级别。
答案 1 :(得分:0)
尝试:
# answers_controller.rb
def new
@question = Question.find(params[:question_id])
@answer = Answer.new(question: @question)
end
# _answer_form.html.erb
<%= form_for([@question, @answer]) do |f| %>
答案 2 :(得分:0)
因为您对嵌套资源使用new_question_answer_path
- url helper,所以需要为它提供question_id:new_question_answer_path(@question)
。另外,我不知道为什么你需要答案的原因不仅仅是嵌套到问题资源中,所以@ davidhu2000提供的是更正确的路由代码。