2015-06-12 45 views
1

我正在調試一個問題,應該是一個相當簡單的CRUD應用程序,我正在採取的課程。事實證明,這比我想象的要困難得多。我試圖在應用中設置它,以便我可以進入問題並選擇編輯或刪除它。刪除工作或多或少都很好。但是當我點擊'編輯'按鈕時,出現以下錯誤:ArgumentError:表單中的第一個參數不能包含零或爲空。 Rails的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

+0

什麼是'url'? – inye

+2

你是否確實在你的數據庫中有一個問題:whit'id = 1'? – inye

+0

我使用Faker爲自己播種數據庫。 – Misanthropos

回答

1

你上你的節目查看有沒有<%= link_to "Delete", @question.delete, class: 'btn btn-danger' %>查看該項目的存儲庫。所以每當你看到show view時,它都會執行@ question.delete並刪除@question。如果你不相信我訪問一個問題,然後刷新頁面,你會看到你的@question已被刪除。將該代碼替換爲<%= link_to "Delete", @question, method: :delete, class: 'btn btn-danger' %>。我看到,在你的git倉庫中,你註釋了編輯操作,不要忘記取消它的註釋。

+1

這是一個古怪的小問題。我在運行delete時沒有註釋「編輯」,但沒有意識到我的link_to語法錯誤。現在它工作得很好。 – Misanthropos

相關問題