Betelhem Elfagid
3 min readApr 19, 2021

--

Image_tag argument Error when using CarrierWave Ruby on Rails

Coding errors could occur in many ways, it could occur from a syntax error which is a simple typo, spelling, grammar or it could occur from a logic error when it doesn’t do what we wanted it to do and there are different factors that trigger different kind of errors in programming and how they manifest and impact the program determines which type of error they are.

Once I read on a random website saying that “No matter your skill level or experience, anyone that writes and deals with computer code will experience coding errors, from time to time”, well in my case often 😊 but definitely learning from them along the way, even if how frustrated I can get sometimes when I couldn’t figure it out quickly but there is always a way and so many helpful people and web out there.

I am currently doing my rails portfolio project for Flatiron School’s Online part time Software Engineering Program, it is called ResRev, it is a simple ruby application that can help users to add and review restaurants.

The app allows users to sign up, log in directly or with third party login link(google & github) and log out.

It has CRUD functionality, users can create, edit and delete their own added restaurants & reviews, they also can see all of the other restaurants and reviews that is created by other users.

To do this project I used carrierwave to upload images for my app and I came across with this simple yet a bit tricky argument error while using the image_tag, I added all the necessary files including carrierwave gem in my gem file, mount_uploader in my model and created image uploader ruby file in my app/uploader folder and make the image uploader class to inherit from CarrierWave::Uploader::Base and added a method to customize the uploaded images, also in my view file on the index page I added the image tag however the error wouldn’t cooperate to go away which makes me stuck before i figure out the solution.

These are the simple solutions to do when below error appears on CarrierWave:-

ERROR -“Nil location provided. Can’t build URI.”

- If there is a user & image attribute is nil, check in your console and delete the user’s image attribute which is nil.

- You can also define a default image inside your image_uploader ruby file.

def default_url(*args)

“default.png”

end

- Else inside your views file where there is your image tag, on top, add a condition where it only displays if there is a corresponding image so that It won’t give you an error even if user’s image attribute is nil.

In my case all I needed was to add below condition on top of the image tag for my code to work.

<% if restaurant.try(:image).try(:url)%>

And I hope one of the mentioned solutions will help you as it helped me with my code.

Happy Coding and stay safe!

--

--

Betelhem Elfagid
0 Followers

Full stack software engineering graduate from Flatiron School.