carrierwave :thumb wrong number of arguments

2019-06-03 17:09发布

问题:

I'm following along with Ryan Bates' carrierwave Railscast http://railscasts.com/episodes/253-carrierwave-file-uploads. At one point, after resizing the images into a thumbnail, he displays the thumbnail with the following code

  <%=  image_tag painting.image_url(:thumb).to_s %>

I'm calling the url method on the profilepic instance variable and trying to get the thumbnail like this

 <%=  image_tag @profilepic.url(:thumb).to_s %>

But I get the error

wrong number of arguments (1 for 0)

It's not expecting the :thumb parameter.

In the image_uploader, I arranged for the thumbnail to be created like this (after installing rmagick)

 version :thumb do
    process :resize_to_limit => [50, 50]
  end

Can anyone explain what I might be doing wrong? I found an SO question on topic Rails: image_tag issue, which explains that the parameter (in this case :thumb) needs to be passed to the url method on the object (and not the object itself). That's what I'm doing, but I'm getting the error.

回答1:

First, the code from Ryan have a typo

<%=  image_tag painting.image_url(:thumb).to_s %>

the correct one is

<%=  image_tag painting.image.url(:thumb).to_s %>

This already posted in the comment of that esp.

For your code, the @profilepic is a obj from model or from carrierwave

e.g. @profilepic = ProfilePic.find :first @profilepic.url is just a method from ProfilePic

The correct syntax for call the url of carrierwave control file is @profilepic.image.url(:thumb)

Hope this not too confusing.