0

So, before asking, I must say I am absolutely new to web programming. I have started studying 2 days ago and my only programming experience is with C language. I am trying to learn ruby on rails through some exercises. This means that I don't know almost anything about the syntax, but I am getting it little by little.

So, the current exercise is: to fetch a JSON file, obtained from a website API, and store its data in a model, which in turn I must display on a table.

Because I have a C experience, I understand this problem as something like 1. creating a new structure, 2. creating a vector for this structure, 3. reading data from a file a storing it in this vector, 4.printing the data. All that is done through writing lines of code in a single .c file and then running it in my terminal.

But things don't seem to work in this manner with rails language. I have read some codes with similar problems but I'm just not quite sure where to write them. I mean, in which files/directories? Because in C we don't have such a thing as multiple files and folders (we just write a one-page program). I am kind of lost.

If someone can help with some general steps, as where/why/what to write, I would be very grateful! There is no need to be completely specific just some overall ideas would be fantastic

Brisão
  • 103
  • 2

1 Answers1

0

Assume that you have obtained a JSON file that contains one single JSON object and that you have read it from the file into memory. Your question does not clarify whether this would already be an issue, so I assume that you can do this conversion (string in file to live object). If not, take a look at this: Serialize Ruby object to JSON and back?

The next thing to do is to turn it into an in-memory ActiveRecord object. Assuming that the json object only contains key/value pairs that are valid for the model in question, you can do it as simple as this:

ar_object = MyModel.new(json_object.to_hash)

Then, save it to the database (which requires a database table that corresponds to you AR model and that has a correct column for each of the keys in your json object:

ar_object.save

Then, when a browser calls the index page of the AR model, e.g. http://localhost:3000/my_models.html, use your controller's index method to fetch all instances of the model from the database:

class MyModelsController < ApplicationController

   def index
     @objects = MyModell.all
     # rails will render /app/views/my_models/index.html.erb by default
   end

end

Then in app/views/my_models/index.html.erb:

<table>
   <thead>
     <tr>
       <th>Col 1 name</th>
       <th>Col 2 name</th>
     </tr>
   </thead>
   <tbody>
     <% for object in @objects do %>
       <tr>
         <td><%= object.col_1 %>
         <td><%= object.col_2 %>
       </tr>
     <% end %>
   </tbody>
 </table>

The HTML might of course look different, depending on your model's attributes and on the templating engine you would prefer (have a look at haml).

Andreas Gebhard
  • 361
  • 2
  • 8
  • thank you very much, this was absolutely helpful. I`ll follow these general steps and hopefully finish my small project. I appreciate a lot your time and patience! – Brisão Nov 20 '20 at 15:01