Rails Error: ActiveRecord::RecordNotFound



我正在从事我的第一个铁轨项目,我遇到了一个持久的问题。我怀疑这与路由有关,但是,我似乎在网上找不到有关它的信息。

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

tl; dr

我想实现的目标

  • 帐户详细的卡显示名称,电话号码和注释。
  • 删除和编辑按钮将允许用户删除或编辑。

发生了什么:

  • 编辑和删除按钮返回一个怪异的参数。
  • 请参阅图像

错误图像,显示导轨获得不同的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

in 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

希望这会有所帮助。

最新更新