我正在調試一個問題,應該是一個相當簡單的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
什麼是'url'? – inye
你是否確實在你的數據庫中有一個問題:whit'id = 1'? – inye
我使用Faker爲自己播種數據庫。 – Misanthropos