简体   繁体   中英

Rails Error: ActiveRecord::RecordNotFound

I am working on my first Rails project and I am running into a persistent issue. 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

What I was trying to achieve

  • Account detail Cards display Name, Phone number, and a note.
  • A delete and edit button would allow users to delete or edit.

What is happening:

  • Edit and Delete buttons return a weird param.
  • see image

Image of error, Showing Rails getting a different 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

<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

<%= 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. But here we need only one instance, so account .

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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