Wednesday, April 6, 2011

scope in Ruby / Sinatra

I am trying to identify a user ID number, to grab a Student row from an ActiveRecord table, but for some reason the 'post' block won't find my :id.

For the 'get', the url is localhost:9456/update/17. It's this 17 I need to pass to the 'post' block to update the database.

I am not sure how to do this. Parse the URL? It seems like I am missing something obvious.

# 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

thanks!

From stackoverflow
  • Are you actually passing the id back? Are you submitting a form via POST? If so, all you should need to do is add an input whose name attribute is id.

    <form action='/student/update' method='post'>
      <input type='hidden' name='id' value='17' />
    </form>
    
    gleddy : thanks for that. works fine now. didn't even think about the hidden field
    Topher Fangio : No prob, any chance you could select my answer as the correct one so others know that it has a solution?

0 comments:

Post a Comment