Displaying a Carrierwave filename in the view

前端 未结 10 1107
暖寄归人
暖寄归人 2020-12-13 23:29

I am trying to display the filename of a Carrierwave attachment in a Rails erb template. The following does not work:

<%= @page.form.filename %>


        
相关标签:
10条回答
  • 2020-12-14 00:04

    I have been able to get the filename via the file internal parameter:

    <%= @page.form.file.filename %>
    
    0 讨论(0)
  • 2020-12-14 00:06

    I'm assuming you've got models like this?

    class Page
      mount_uploader :form, FormUploader
    end
    

    If so you should be able to call:

    @page.form.url
    @page.form.filename
    

    Are you sure you've uploaded/attached the file correctly? What do you see when you inspect @page.form? Remember, the attachment will not be saved until you've fully processed the upload.

    0 讨论(0)
  • 2020-12-14 00:07

    The Carrierwave docs might be a bit off, but recommended way seems to be:

    @page.form.file.identifier
    
    0 讨论(0)
  • 2020-12-14 00:08

    @adamonduty's solution is great. Another solution I used before, just create a method on the model:

    def name
      file.path.split("/").last
    end
    
    0 讨论(0)
提交回复
热议问题