kemalcr / kemal

Fast, Effective, Simple Web Framework
https://kemalcr.com
MIT License
3.65k stars 189 forks source link

Cannot pass data to template view, only to subview #696

Closed frojnd closed 2 weeks ago

frojnd commented 2 weeks ago

Description

Consider following:

  get "/rooms" do |env|
    room_id = env.params.query["id"]?
    if room_id
      env.redirect "/room/"
    else
      path = env.request.path
      rooms_data = get_rooms
      render "src/views/room/rooms.ecr", "src/views/layout.ecr"
    end
  end

I would expect to have rooms_data inside "src/views/layout.ecr" But I get: Error: undefined local variable or method 'rooms_data' for Household:Module when calling <%= rooms_data %>

Also how can I pass besides rooms_data also path to both views?

Thank you.

frojnd commented 2 weeks ago

I have a bug somewhere in my project. Just tired with a blank kemal project and data is being transferred among the views...

frojnd commented 2 weeks ago

So if anyone else finds this issue. Problem was with all the other routes that were rendering layout.ecr or rooms.ecr but get route itself didn't have |env| defined for example:

 get "/" do
    if true
      rooms_data = get_rooms(worst = 3)
      render "src/views/home.ecr", "src/views/layout.ecr"
    else
      "ONBOARDING"
    end
  end

After adding |env| to the route problem solved itself.