繁体   English   中英

Rails错误:ActiveRecord :: RecordNotFound

[英]Rails Error: ActiveRecord::RecordNotFound

我正在做我的第一个Rails项目,并且遇到了一个持续存在的问题。 我怀疑它与路由有关,但是,我似乎无法在线找到任何有关它的信息。

我认为它是一个相当简单的修复程序,因此请看一看,让我知道是否可以提供帮助。

TL; DR

我想要达到的目标

  • 帐户详细信息卡片显示姓名,电话号码和备注。
  • 删除和编辑按钮将允许用户删除或编辑。

怎么了:

  • 编辑和删除按钮返回一个奇怪的参数。
  • 看图片

错误图片,显示Rails获得了另一个ID

调节器

  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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM