0

I'm using Rails 4.0.2 with paperclip for image upload in my project. Also I need to send a full image path of paperclip. So I can do it with add new field and set image path manually in my show controller method for particular record.

show

def show
  respond_to do |format| 
    format.html
    format.json { :json => JSON::parse(@demo.to_json.merge("new_field" => @demo.image_url.url).to_json}
  end
end

When I view Json for any of my record, this is will showing good.

{
  id: "1",
  name: "demo",
  new_field: "/demo/1/original/file.jpg"
}

In same scenario, I need to get the full image path of paperclip image for all records when I am requesting to index method on controller

index

def index 
  @demos = Demo.all
  respond_to do |format|
    format.html
    format.json { :json => Demo.all.to_json}
  end
end

I tried some of codes, but I don't know how exactly to write

def index 
  @demos = Demo.all
  @demos.each do |demo|
     new_field = {"new_field" => @demo.image_url.url}
     # After I stucked with logic, how to uppend with 'demo'.
  end

  respond_to do |format|
    format.html
    format.json { :json => Demo.all.to_json}
  end
end

How do I iterate my individual Demo model and How to merge full image path into each record.

1
  • @GraemeMcLean : Actually the problem is not from path or url. If I iterate @demos.each do |demo| like this, I got @demo.image_url.url actual result. I need to know how to append or merge with that into existing instance object Commented May 27, 2014 at 6:54

5 Answers 5

3

I found the solution for my question,

def index 
  @demos = Demo.all
  @demos_data = []
  @demos.each do |demo|
    new_field = {"new_field" => @demo.new_field.url}
    demo = JSON::parse(demo.to_json).merge(new_field)
    @demos_data << demo
  end

  respond_to do |format|
   format.html
   format.json { :json => @demos_data}
  end
end
Sign up to request clarification or add additional context in comments.

Comments

1

I suggest you to use two approaches, 1)use active model serializer to expose json response. 2) use jbuilder library to expose custom fields. Still you need help please let me know.

1 Comment

I can't get it clearly. Why I need to do that? Let me explain, 1.how to iterate demo by demo and 2. and find each image of demo. then 3. how to I append that with existing one?
0

Try this:-

def index 
  @demos = Demo.all
  @demos_data = []
  @demos.each do |demo|
    demo["new_field"] = @demo.image_url.url
    @demos_data << demo
  end

  respond_to do |format|
   format.html
   format.json { :json => @demos_data}
  end
end

4 Comments

I think new_field is not in demo, then how its possible demo['new_field'] = ....?
it will an extra field named as new_field in the json and can be viewed in the browser by appending .json in the url
I'm getting undefined method 'new_field' on that same line..
I'm using rails 4.0.2. May be it won't work on updated version. No problem at all. Now I got solution from your idea. Thanks :)
0

maybe you can try:

def index 
  @demos = Demo.all
  @demos.map do |demo|
    new_field = {"new_field" => @demo.image_url.url}
    demo.attributes.merge(new_field)
  end

  respond_to do |format|
    format.html
    format.json { :json => @demos}
  end
end

attributes method returns a hash of all the object attributes, just need merge new key-value into the returned hash.

1 Comment

You should add an explanation to your answer.
0

Another way of doing it is in your controller where you are rendering the json

render json: @merchants,
      include: {
        offers: {
          except: [:created_at, :updated_at],
          include: {
            categories: {
              except: [:created_at, :updated_at]
            }
          }
        },
        location: {
          methods: :country_name,
          except: [:created_at, :updated_at]
        }
      },
      except: [:created_at, :updated_at]

Note the methods: :country_name, there you can render methods from your model as json attributes. and through include: ... you can eager load and render related models.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.