ArgumentError:表单中的第一个参数不能包含nil或为空。 Rails CRUD app,

时间:2015-06-12 01:06:26

标签: ruby-on-rails activerecord

我正在调试一个问题,我应该选择一个相当简单的CRUD应用程序。它变得比我想象的更难。我试图在应用中设置它,以便我可以进入问题并选择编辑或删除它。删除工作或多或少都很好。但是当我点击“编辑”时出现以下错误:按钮:

ActiveRecord::RecordNotFound in QuestionsController#edit - Couldn't find Question with 'id'=1引用我的问题控制器中的编辑方法。

这是错误:

Extracted source (around line #13):

11 </div>
12 <div class="col-md-8">
13   <%= form_for @question do |f| %>
14     <div class="form-group">
15       <%= f.label :title %>
16       <%= f.text_field :title, class: 'form-control', placeholder:        "Enter question title" %>

我的控制器:

 class QuestionsController < ApplicationController
  def index
    @questions = Question.all
  end

  def new
    @question = Question.new
  end

  def create
    @question = Question.new(params.require(:question).permit(:title, :body, :resolved))
      if @question.save
        flash[:notice] = "Question was saved."
        redirect_to @question
    else
       flash[:error] = "There was an error in saving your question. Please ask again."
        render :new
     end
  end

  def show
    @question = Question.find(params[:id])
  end

  def new
    @question = Question.new
  end

  def edit
    @question = Question.find(params[:id])
  end

  def update
    @question = Question.find(params[:id])
    if @question.update_attributes(params.require(:post).permit(:title, :body))
      flash[:notice] = "Post was updated."
      redirect_to @question
    else
      flash[:error] = "There was an error saving the post. Please try again."
      render :edit
    end
  end

   def create
    if      @question.update_attributes(params.require(:question).permit(:title, :body, :resolved))
      flash[:notice] = "Question was updated."
      render :edit
      redirect_to @question
    else
      flash[:error] = "There was an error saving your question. Please try again."
      render :new
    end
  end

  def delete
    @question = Question.find(params[:id])
    @question = Question.destroy
    redirect_to @question
  end
end

```

它应该影响的观点:

<h1><%= @question.title %></h1>

<p><%= @question.body %></p>

<%= link_to "Edit", edit_question_path(@question), class: 'btn btn-success' %>

<%= link_to "Delete", @question.delete, class: 'btn btn-danger' %>

我的路线档案:     Rails.application.routes.draw做

  resources :posts
  resources :advertisements
  resources :questions
  resources :answers

  get 'about' => 'welcome#about'

  root to: 'welcome#index'
end

您可以给予我任何帮助,我将不胜感激。我还在学习所有这些。

您可以在https://github.com/InsomniaNoir/bloccit

查看此项目的存储库

1 个答案:

答案 0 :(得分:1)

您的节目视图中有<%= link_to "Delete", @question.delete, class: 'btn btn-danger' %>。因此,每次获得show视图时,它都会执行@ question.delete并删除@question。如果您不相信我访问问题然后刷新页面,您将看到您的@question已被删除。将该代码替换为<%= link_to "Delete", @question, method: :delete, class: 'btn btn-danger' %>。我在你的git存储库中看到你注释掉了编辑动作,不要忘记取消它。