开发者

Sinatra/Rack params[] in before block

开发者 https://www.devze.com 2023-03-22 04:29 出处:网络
I\'m making a site using Sinatra, everything is going well, or was until I needed to access the params[] hash in the be开发者_如何转开发fore block.

I'm making a site using Sinatra, everything is going well, or was until I needed to access the params[] hash in the be开发者_如何转开发fore block.

Basically, I'm trying to do this:

before do
  if params[:forum_id]
    @forum = Forum.find(params[:forum_id])
    build_breadcrumbs(@forum.parents)
    # ... more code, snipped to keep it short
  end
end

But the thing is, I can't call the params[] hash in the before block, does anyone have any ideas?

The reason I'm putting this code in the before block is because I'd rather not have to go and put it in every one of my get and post blocks for every page.


From the docs:

Before filters are evaluated before each request within the same context as the routes will be and can modify the request and response.

Since this happens before the actual request, you can't access the request parameters. What you can do is put the repetitive code into a method and call that in your route blocks.


This is not true anymore. I just tested it. You can now access params in before {...}

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号