1

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

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

我不知道该怎么做。解析网址?好像我错过了一些明显的东西。

# 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

谢谢!

4

1 回答 1

5

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

<form action='/student/update' method='post'>
  <input type='hidden' name='id' value='17' />
</form>
于 2009-12-17T21:58:01.507 回答