gpt4 book ai didi

ruby - Ruby/Sinatra 中的作用域

转载 作者:太空宇宙 更新时间:2023-11-03 17:41:37 25 4
gpt4 key购买 nike

我正在尝试识别用户 ID 号,以从 ActiveRecord 表中获取学生行,但由于某种原因,“post” block 找不到我的 :id。

对于“get”,url 是 localhost:9456/update/17。这是 17 我需要传递给“post” block 来更新数据库。

我不知道该怎么做。解析网址?似乎我遗漏了一些明显的东西。

# update user page
get '/update/:id' do
@student = Student.find(params[:id]) #this returns ID=17
erb :update
end

#update user submit
post '/update' do
@student = Student.find(params[:id]) #this line doesn't work
@student.name = (params[:name])
@student.email = (params[:email])
@student.save
redirect '/'
end

谢谢!

最佳答案

你真的传回了 id 吗?您是通过 POST 提交表单吗?如果是这样,您需要做的就是添加一个输入,其 name 属性为 id

<form action='/student/update' method='post'>
<input type='hidden' name='id' value='17' />
</form>

关于ruby - Ruby/Sinatra 中的作用域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1924854/

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