Ruby/Sinatra中的范围

gle*_*ddy 1 ruby parameters sinatra

我正在尝试识别用户ID号,从ActiveRecord表中获取学生行,但由于某种原因,"post"块将找不到我的:id.

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

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

# 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
Run Code Online (Sandbox Code Playgroud)

谢谢!

Top*_*gio 5

你真的经过了id吗?您是通过提交表格POST吗?如果是这样,您需要做的就是添加name属性为的输入id.

<form action='/student/update' method='post'>
  <input type='hidden' name='id' value='17' />
</form>
Run Code Online (Sandbox Code Playgroud)