【问题标题】:Rails - No route matches - missing required keysRails - 没有路线匹配 - 缺少必需的键
【发布时间】:2016-08-26 03:00:17
【问题描述】:

我对 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::Error(没有路由匹配 {:action=>"new", :controller=>"answers", :id=>"4"} 缺少必需的键:[:question_id]):

所以我认为我需要做的就是将 question_id 传递给它。但是,我该怎么做呢?

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    对于你的路线,你想像这样嵌套

    resources :microposts,          only: [:show, :create, :destroy] do
      resources :questions do
        resources :answers
      end
    end
    

    这会让你的网址像

    /microposts/:micropost_id/questions/:question_id/answers/answer_id
    

    根据您的描述,这就是您要找的。这可能会解决您的问题。或者让我知道我是否误解了你的问题。

    然而,这不是一个好的做法,你永远不想嵌套超过一层。

    【讨论】:

    • 是的,我还读到嵌套超过一层是不好的。我需要找到一种将 question_id 传递给 new_question_answer_path 的方法。我试过 new_question_answer_path(@question) 但它给了我同样的错误。
    【解决方案2】:

    试试:

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

    你也可以看看https://stackoverflow.com/a/4537247

    【讨论】:

    • 我现在有这个错误:表单中的第一个参数不能包含 nil 或为空
    • @BrianT。请检查@question@answer_answer_form.html.erb 中是否为空或为空
    • 当我在控制台输入@question@answer时,两者都是nil。
    • @BrianT。我认为您需要将它们传递给_answer_form.html.erb,例如,将它们设置在相应的控制器操作中。
    【解决方案3】:

    因为您使用new_question_answer_path - 嵌套资源的url helper,您需要为其提供question_id:new_question_answer_path(@question)。此外,我看不出您需要答案的原因不仅是嵌套在问题资源中,因此@davidhu2000 提供了更正确的路由代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-07
      • 1970-01-01
      • 2014-12-25
      • 1970-01-01
      相关资源
      最近更新 更多