ruby-on-railsrubyrails-activestoragenomethoderror

NoMethodError (undefined method `last' for true:TrueClass):


I got NoMethodError (undefined method `last' for true:TrueClass): from app/controllers/posts_controller.rb:71:in `uploads'

Here is the script

  def uploads 
    @post = current_user.posts.friendly.find(params[:id])
    a = @post.images.attach(params[:file])
    render json: {url: url_for(a.last)}
  end

Not sure what am I doing wrong. Any advice?

Solution

  • I imagine the 'attach' method is returning 'true' or 'false' as to whether it was successful or not rather than assigning the file to the variable.

    Personally I'd remove the variable 'a', then just use:

    url_for(@post.images.last)