gpt4 book ai didi

javascript - 如何修复 Rails 'ActiveRecord::RecordNotFound (Couldn' t 找不到没有 ID 的程序)'

转载 作者:行者123 更新时间:2023-11-30 13:56:06 25 4
gpt4 key购买 nike

我正在对我的 Rails API 进行初步测试,方法是对我的后端执行提取并使用 console.log() 在我的浏览器控制台中查看结果。当我向我的命名空间 url 发出请求以查看程序列表时出现我的错误。

我已经检查了 Controller 的显示方法,以及我的路线和模型。我知道它返回的是 html 而不是 JSON,这就是部分提取中断的原因。

App.js

componentDidMount(){
fetch('http://localhost:3000/api/v1/programs')
.then(res => res.json())
.then(json => console.log(json))

程序 Controller .rb

class Api::V1::ProgramsController < ApplicationController
def index
@programs = Program.all
render json: @programs
end

def show
@program = Program.find(params[:id])
render json: @program
end

private

def program_params
params.require(:program).permit(:url, :name, :network, :image)
end
end

我希望看到我放置在 seeds.rb 文件中的 18 个程序的列表,但我从 ActiveRecord 收到错误消息“无法找到没有 ID 的程序”。我确实尝试在我的 program_params 方法中将 :id 传递给 .permit() 但也没有成功。

最佳答案

我认为您的 api/v1 url 将在 routes.rb 中丢失或出现错误。你能给我看看你的 routes.rb 文件吗?

additionally i have checked your source code in github after you write comment to this answer and I find wrong word you have coding resource in routes.rb and it's wrong word

you have to change resource to resources in config/routes.rb

2.5 Singular Resources Sometimes, you have a resource that clients always look up without referencing an ID. For example, you would like /profile to always show the profile of the currently logged in user. In this case, you can use a singular resource to map /profile (rather than /profile/:id) to the show action:

https://guides.rubyonrails.org/routing.html#singular-resources

我建议你必须阅读 Rails 指南 https://guides.rubyonrails.org/

关于javascript - 如何修复 Rails 'ActiveRecord::RecordNotFound (Couldn' t 找不到没有 ID 的程序)',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57280282/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com