ruby-on-railsrubyjsonruby-on-rails-4flickr

Parsing a static JSON file into a Rails object


I'm trying to parse a static JSON file from my root directory into an already predefined object, but I'm getting confused on how to get the object to "read" each attribute within the JSON file and display it as its own. I hope I'm not making this more confusing from what it is?

Some of my code:

   class PostsController < ApplicationController
  # before_action :set_post, except: [:index, :show]

  # @@posts = File.read('app/assets/javascripts/flickr_feed.json')
  # @posts = JSON.parse(string)

  # GET /posts
  # GET /posts.json
  def index
    @posts = Post.all
    respond_to do |format|
      format.html
      format.json { render json: @@posts }
      # format.json { render json: @@posts }
    end
  end

  # GET /post/1
  # GET /post/1.json
  def show
    @post = @post.assign_attributes JSON.parse(File.read('app/assets/javascripts/flickr_feed.json'))
    respond_to do |format|
      format.html
      format.json { render json: @post }
    end
  end
  ...

If I go to localhost:3000/posts.json I see the desire output..

{
"title": "Recent Uploads tagged potato",
"link": "https://www.flickr.com/photos/tags/potato/",
"description": "",
"modified": "2015-11-21T08:41:44Z",
"generator": "https://www.flickr.com/",
"posts": [ // before was "items":
 {
  "title": "Hokkaido potato with butter",
  "link": "https://www.flickr.com/photos/taking5/22873428920/",
  "media": {"m":"https://farm6.staticflickr.com/5813/22873428920_3cac20cc47_m.jpg"},
  "date_taken": "2015-07-18T08:16:24-08:00",
  "description": " <p><a href=\"https://www.flickr.com/people/taking5/\">Taking5<\/a> posted a photo:<\/p> <p><a href=\"https://www.flickr.com/photos/taking5/22873428920/\" title=\"Hokkaido potato with butter\"><img src=\"https://farm6.staticflickr.com/5813/22873428920_3cac20cc47_m.jpg\" width=\"240\" height=\"180\" alt=\"Hokkaido potato with butter\" /><\/a><\/p> <p>Yummy.<\/p>",
  "published": "2015-11-21T08:41:44Z",
  "author": "nobody@flickr.com (Taking5)",
  "author_id": "58375502@N00",
  "tags": "japan hokkaido potato hakodate morningmarket"
 }
]
}
 ...

But if I go to the posts#index I don't see anything. I know I'm not parsing the data properly, but I'm getting confused on how to do that.

Summary: I would like to parse each item from JSON file so as to be able to do post.title, post.description, etc.

Edit: Update on code in the controller.


Solution

  • You can try:

    @post = Post.new
    @post.assign_attributes JSON.parse(File.read('app/assets/javascripts/flickr_feed.json'))
    

    If you use protected_attributes gem, attributes set using this manner must be defined with attr_accessible in the model or parameter without_protection must be used.

    Edit:

    def index
      @posts =
        JSON.parse(File.read('app/assets/javascripts/flickr_feed.json'))["posts"].inject([]) do |_posts, post_attrs|
          _posts << Post.new(post_attrs)
        end
      respond_to do |format|
        format.html
        format.json { render json: @posts }
      end
    end