I'm making a site using Sinatra, everything is going well, or was until I needed to access the params[]
hash in the before
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 {...}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.