I'm trying to make a custom tag for RedCloth. However, I need to fetch some data from the database and base the tag on that. I.e. when writing "image. 1" it should fetch a user uploaded picture from the database with id #1.
But I can't figure out how to pass current_user (or other crucial instance variables from the controller) to the module. I imagine that the code could look something like this:
module RedclothExtension
def image(opts)
@image = User.find(current_user.id).images.where(id: opts[:text])
"#{@image.title}"
end
end
As I see it there are two ways:
- Passing the needed variables to the module.
- Fetching the instance variables from the controller.
But how do i do this exactly? Or is there a smarter way?