Browse files

Merge pull request #101 from pencilcheck/patch-1

Add a section for configuring routes for the carrierwave url
  • Loading branch information...
rmm5t committed Jun 10, 2013
2 parents 3536157 + ae3ecba commit bb7b1fd21e3a441c17433661a1ba287cd0b2de81
Showing with 33 additions and 0 deletions.
  1. +33 −0
@@ -98,6 +98,39 @@ CarrierWave.configure do |config|
+## Route configuration
+If you follow the instruction to this point, the uploaded images will be stored to GridFS, but since most of us would still be using
+carrierwave's api to retrieve url from the resource like so .avatar.url, it would not work since the url does not point
+to the method defined in the previous section for serving the content.
+One small trick to get around this automatically is to do a simple regex matching in routes.rb.
+Before we begin, go to initializers/carrierwave.rb
+to remove `config.root` if you have one, and add `config.grid_fs_access_url = '/uploads/grid'`
+grid_fs_access_url is used to add a prefix to the url returned from carrierwave for our purpose,
+and it's crucial to the regex method described below.
+Let's assume the attribute mounted is avatar and the model is user and controller is called gridfs,
+in the uploader file (e.g. avatar_uploader.rb) return
+in your store_dir method.
+Now add those lines below in routes.rb
+`match '/uploads/grid/user/avatar/:id/:filename' => 'gridfs#avatar'`
+Now in your view your "model.avatar.url" should show image correctly.
+### Different versions
+If you need to include different versions like thumb, add this in your routes.rb
+`match '/uploads/grid/user/avatar/:id/:filename' => 'gridfs#thumb_avatar', constraints: {:filename => /thumb.*/}`
## Version differences
| Version | Notes |

0 comments on commit bb7b1fd

Please sign in to comment.