[英]Rails Error: ActiveRecord::RecordNotFound
I am working on my first Rails project and I am running into a persistent issue. 我正在做我的第一个Rails项目,并且遇到了一个持续存在的问题。 I suspect it has something to do with the routing, however, I can't seem to find anything about it online.
我怀疑它与路由有关,但是,我似乎无法在线找到任何有关它的信息。
I assume it a rather simple fix, so please take a look and let me know if you can help. 我认为它是一个相当简单的修复程序,因此请看一看,让我知道是否可以提供帮助。
TL;DR TL; DR
What I was trying to achieve 我想要达到的目标
What is happening: 怎么了:
Image of error, Showing Rails getting a different ID 错误图片,显示Rails获得了另一个ID
Controller 调节器
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 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 %>
Routes 路线
Rails.application.routes.draw do
get 'welcome/index'
resources :articles do
resources :comments
end
resources :accountdetails
root 'welcome#index'
end
In you index.html.erb replace following 在您的index.html.erb中替换以下内容
<%= link_to "edit", edit_accountdetail_path(@accountdetail) %>
<%= link_to 'Inspect', accountdetail_path(@accountdetail) %>
with 同
<%= link_to "edit", edit_accountdetail_path(account) %>
<%= link_to 'Inspect', accountdetail_path(account) %>
@accountdetail
was providing you all the records of account, as it was firing select query in controller. @accountdetail
为您提供了所有帐户记录,因为它在控制器中触发了选择查询。 But here we need only one instance, so account
. 但是这里我们只需要一个实例,所以请使用
account
。
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.