Search code examples
rubysinatracruderbsequel

Reference database column with variable in sequel sinatra


How do I refernce the column in the database with the column variable in the url/route. The column varible is a string coming from a webpage, and I have to reference the column with a hash key.

Sinatra route:

get '/users/update/:id/:column/:value' do
  User.where(id: params[:id]).update(column: params[:value])
  redirect back
end

Html:

<a href="/users/update/<%= user.id %>/status/3">Value</a>

Solution

  • If you are looking to use params[:column] as a variable key in the update you can use the hash rocket syntax:

    User.where(id: params[:id]).update(params[:column] => params[:value])