jbuilder icon indicating copy to clipboard operation
jbuilder copied to clipboard

ActionView::Template::Error (no implicit conversion of String into Integer)

Open justin808 opened this issue 7 years ago • 2 comments

There are still cases where the error message:

ActionView::Template::Error (no implicit conversion of String into Integer)

is extremely obtuse.

I'm opening this issue so that others when they see this can paste the offending code. I'd paste my example, but I forgot what it was. Then another team member of mine hit the same error and got stuck for a long time.

The process I used to solve the issue is:

  1. Comment out lines in the template to find the offending line or lines.
  2. Read examples super carefully and look for errors.

justin808 avatar Mar 18 '17 21:03 justin808

I spent a little while scratching my head on this error tonight before I found this thread. Thought I'd leave an example, in case others find it too. I'm using a few nested partials.

Can replicate it pretty easy by doing something like this:

#articles/_index.json.jbuilder
json.array!(articles) do |article|
  json.partial! '/shared/models/article', article: article
end

json.partial! '/shared/pagination', model: articles


#shared/_pagination.json.jbuilder
json.set! :pagination do
  json.set! :currentPage,  model.current_page
  json.set! :totalPages,    model.total_pages
  json.set! :itemCount,    model.total_count
end

This results in the error:

ActionView::Template::Error (no implicit conversion of String into Integer):
    1: json.set! :pagination do
    2:   json.set! :currentPage,  model.current_page
    3:   json.set! :totalPages,   model.total_pages
    4:   json.set! :itemCount,    model.total_count

10 points if you noticed what I did wrong.. Turns out it was being caused by the return type on the original partial articles/_index.json.jbuilder. The partial is returning both an array (of articles) and an object (pagination) and attempting to assign both values to a parent object 🤦

Changing the first line to return an object rather than an array fixed the issue:

json.articles(articles) do |article|

Anyways, hope it helps.

2potatocakes avatar Aug 03 '20 13:08 2potatocakes

This saved me hours of banging my head against the wall... THANK YOU @2potatocakes

dennismonsewicz avatar Aug 23 '21 18:08 dennismonsewicz