Closed vikramksingh closed 9 years ago
Having url helpers inside you model is generally considered bad practice.
You can simply remove then include Rails.application.routes.url_helpers
and def to_jq_upload
from the model and implement similar logic in your controller, eg. using a protected method render_upload(upload)
that calls the same stuff as to_jq_upload
on the upload model instance.
If you share this code across multiple controllers, you could move it into a decorator/presenter, that gets passed the required context from the controller. Some popular decorator gems are representable and draper, but don't worry about that if youä#re jsut getting started.
Thanks Felix for a meaningful suggestion. I tried for it too, but with this my upload path will not be dynamic as the following code is still in model, I believe it can be done with helper or an uploader file but I am not getting how to create it and integrate it instead of model.
has_attached_file :upload, :url => "/controllers/original/:basename.:extension", :path => "/files/docs/:basename.:extension"
Is still in model where i need local variables to make path dynamic. even I dont get current user data in model so that i can create with logged in user id.
You should look into paperclip url / path interpolation for the available interpolators. I'm pretty sure you can also write your own.
Maybe this Stack Overflow post will point you in the right direction: http://stackoverflow.com/questions/4041373/paperclip-custom-path-and-url
@vikram-hermes Can this issue be closed?
Yes, I got it done with interpolations Thanks
Is it possible to have controller function to upload files instead of having model ? I need to use local variables to make path dynamic which is not possible when using model for upload.
following is my code attr_accessible :upload has_attached_file :upload, :url => "/controllers/original/:basename.:extension", :path => "/files/docs/:basename.:extension"
include Rails.application.routes.url_helpers
def to_jq_upload { "name" => read_attribute(:upload_file_name), "size" => read_attribute(:upload_file_size), "url" => upload.url(:original), "delete_url" => upload_path(self), "delete_type" => "DELETE" } end