Question

So I loaded a Badge with an image just fine on rails 3.2

badges = [  {id: 1, name: 'Democratic-Society', description: "Voted for two bands", image: "demo_society2.gif"}
 ]

badges.each do |badge|
  Merit::Badge.create!(badge)
end

But I just updated my code server to Rails 4.1... and now it gives the following error:

undefined method `image' for #<Merit::Badge:0x007f57949ba8f0>

I didn't change the code AT ALL. Didn't touch the merit initializer... or the application.rb... nothing. Can't quite figure out why it's not working. Here's the code from application.rb:

<% current_user.badges.each do |badge| %>
<%= image_tag (badge.image), :alt => badge.description, :float => "left" %>
<% end %>

Any help would be appreciated.

Was it helpful?

Solution

Alright the solution (in case anyone else runs into this) has to do with "custom_fields". Any custom fields have to be referenced as such (for some reason you didn't really need to do this in Rails 3.2 but you do in Rails 4.1)

So all I did was change the code to this:

badges = [  {id: 1, name: 'Democratic-Society', description: "Voted for two bands", custom_fields: {image: "demo_society2.gif"} }]

And then in my view:

<%= image_tag (badge.custom_fields[:image]), :alt => badge.description, :float => "left" %>

And VOILA!

Credit to marcosgugs over at github for the solution.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top