Search code examples
parameterssinatrakemal

Using params in view in kemal


I have a simple ruby/sinatra app I am attempting to migrate to crystal/kemal. When user submits form choice, the sinatra version POSTs the params as a hash

{"category"=>["selection1", "selection2",..]}

I then pass the selections to the view by render index.erb in the post route. However in kemal I can collect the params but cannot figure out how to access them in the view. I keep getting the error undefined local variable or method.

How do I make the view access the submitted params?

Here is the sinatra code for the post route:

post '/' do
  if params.values.flatten.length < 1
    flash[:warning] = "Please choose at least one AED"
    redirect '/'
  else
    params
    erb :index
  end
end

and the corresponding kemal code:

post "/" do |env|
  selected_drugs = env.params.body.fetch_all("drugs")
  if selected_drugs.size < 1
    env.flash["warning"] = "Please choose at least one AED"
    render "src/views/index.ecr"
  else
    selected_drugs
    render "src/views/index.ecr"
  end
end
Kemal.run

Solution

  • Did not define the variable holding params in all routes that used index.ecr. Once that was done it worked. Interesting that I didn't have to define it in some routes for sinatra, though.