0

I'm aware of the other questions pertaining to this subject, but none seem to help. I want the paperclip image url passed to json so I can render it in a reactjs component. I obviously cannot use Rails' image_tag helper.

I've defined this method in my items model

def image_url
  image.url(:thumb)
end

And this in my controller

def index
  @items = Item.all   
  render :json => @items.to_json(:methods => [:image_url])
end

But literally all that does is replace the rendered page with json. How should I go about this? It doesn't make sense to create a migration and model validation specifically for the image url.

Community
  • 1
  • 1
calyxofheld
  • 1,538
  • 3
  • 24
  • 62

1 Answers1

0

Just needed to format the html.

respond_to do |format|
  format.html
  format.json { render :json => @items.to_json(:methods => [:image_url]) }
end

Still doesn't solve the part where I'm trying to use that method in a reactjs component, but that's another issue.

calyxofheld
  • 1,538
  • 3
  • 24
  • 62