Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
justin808 opened this issue Mar 18, 2017 · 2 comments

Comments

@justin808
Copy link
Contributor

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.
@2potatocakes
Copy link

2potatocakes commented Aug 3, 2020

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.

@dennismonsewicz
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants