[英]Rails Error: ActiveRecord::RecordNotFound
我正在做我的第一個Rails項目,並且遇到了一個持續存在的問題。 我懷疑它與路由有關,但是,我似乎無法在線找到任何有關它的信息。
我認為它是一個相當簡單的修復程序,因此請看一看,讓我知道是否可以提供幫助。
TL; DR
我想要達到的目標
怎么了:
調節器
class AccountdetailsController < ApplicationController
def index
@accountdetail = Accountdetail.all
end
#I can't find the ID to show the relevent card.
def show
@accountdetail = Accountdetail.find(params[:id])
if @accountdetail.nil?
redirect_to accountdetail_path
end
end
def new
@accountdetail = Accountdetail.new
end
def edit
@accountdetail = Accountdetail.find(params[:id])
end
def create
@accountdetail = Accountdetail.new(accountdetail_params)
if @accountdetail.save
redirect_to @accountdetail
else
render 'new'
end
end
#it affects this
def update
@accountdetail = Accountdetail.find(params[:id])
if @accountdetail.update(accountdetail_params)
redirect_to accountdetail
else
render 'edit'
end
end
#and this
def destroy
@accountdetail = Accountdetail.find(params[:id])
@accountdetail.destroy
redirect_to accountdetail_path
end
private def accountdetail_params
params.require(:accountdetail).permit(:first_name, :last_name, :phone, :notes, :id)
end
end
Index.HTML.ERB
<div class="ui card">
<div class="content">
<a class="header"><%= account.first_name %> <%= account.last_name %> </a>
<div class="meta">
<span class="date"><%= account.phone %></span>
<strong><p><%= account.notes %></p></strong> <br>
<%= link_to "edit", edit_accountdetail_path(@accountdetail) %>
<%= link_to 'Inspect', accountdetail_path(@accountdetail) %>
</div>
</div>
</div>
<% end %>
路線
Rails.application.routes.draw do
get 'welcome/index'
resources :articles do
resources :comments
end
resources :accountdetails
root 'welcome#index'
end
在您的index.html.erb中替換以下內容
<%= link_to "edit", edit_accountdetail_path(@accountdetail) %>
<%= link_to 'Inspect', accountdetail_path(@accountdetail) %>
同
<%= link_to "edit", edit_accountdetail_path(account) %>
<%= link_to 'Inspect', accountdetail_path(account) %>
@accountdetail
為您提供了所有帳戶記錄,因為它在控制器中觸發了選擇查詢。 但是這里我們只需要一個實例,所以請使用account
。
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.