Ruby On Rails

8 min

Ruby on Rails API Develaopment- Exmple of Rails API

Ruby on Rails API Develaopment- Exmple of Rails API

Agnesh ( ROR Developer )

Sachin Gevariya

Founder and Technical Director

Side-by-side comparison showing Rails CSV export memory usage: naive in-memory approach consuming 76 MB+ versus streaming approach holding steady at 1 MB for 1 million rows

Creating an API with Ruby on Rails is simple and quick. The nature of Ruby allows it to use external services easily, which is why it is a great idea to develop an API using Ruby on Rails.

When a developer says that they are using Rails as the API, it indicates that they are using Ruby on Rails Development to build the backend, which is shared between the web application and native applications. Let’s see how you can develop an API using RoR.

What is Ruby on Rails?

The best part about Ruby on Rails is that it is open-source software. Users can utilize Ruby on Rails to develop applications or collaborate to bring change to their code.

Ruby on Rails focuses on opting for convention rather than configurations and optimized systems for programmers. Ruby on Rails works excellently with other technologies like Java, CSS, and HTML.

Example: Ruby on Rails API Development Steps

Step 1: API creation using Rails

To initiate the creation of an API, one must first write the below-written code in the terminal of any chosen directory. Using the code written below will create an API named secret_menu_api

$ rails <span class="token keyword keyword-new" style="box-sizing: border-box; color: rgb(0, 119, 170);">new</span> secret_menu_api –<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">-</span>api <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">-</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">-</span>database<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span>postgresql
$ rails <span class="token keyword keyword-new" style="box-sizing: border-box; color: rgb(0, 119, 170);">new</span> secret_menu_api –<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">-</span>api <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">-</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">-</span>database<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span>postgresql
$ rails <span class="token keyword keyword-new" style="box-sizing: border-box; color: rgb(0, 119, 170);">new</span> secret_menu_api –<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">-</span>api <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">-</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">-</span>database<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span>postgresql

You must type the below-written code in the terminal to open the API menu.

$ cd secret_menu_api code
$ cd secret_menu_api code
$ cd secret_menu_api code
Step 2: Enabling CORS (Cross-Origin Resource Sharing)

CORS enables other people to access the API. To prevent other people from accessing the API, one can disable the CORS. To open the cors.rb file, you need to open the file explorer where the Rails API created now is present.

config <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&gt;</span> initializers <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&gt;</span> cors<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>rb
config <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&gt;</span> initializers <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&gt;</span> cors<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>rb
config <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&gt;</span> initializers <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&gt;</span> cors<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>rb

You must uncomment the lines and change the code from origins ‘example.com’ to origins’*’.

Rails<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>application<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>config<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>middleware<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>insert_before <span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">0</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> Rack<span class="token double-colon punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">::</span>Cors <span class="token keyword keyword-do" style="box-sizing: border-box; color: rgb(0, 119, 170);">do</span>
  allow <span class="token keyword keyword-do" style="box-sizing: border-box; color: rgb(0, 119, 170);">do</span>
    origins <span class="token string-literal" style="box-sizing: border-box;"><span class="token string" style="box-sizing: border-box; color: rgb(102, 153, 0);">'*'</span></span>
    resources <span class="token string-literal" style="box-sizing: border-box;"><span class="token string" style="box-sizing: border-box; color: rgb(102, 153, 0);">'*'</span></span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span>
    headers<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:any</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span>
    methods<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> <span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">[</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:get</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:post</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:put</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:patch</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:delete</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:options</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:head</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">]</span>
  <span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span>
<span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span

Rails<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>application<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>config<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>middleware<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>insert_before <span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">0</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> Rack<span class="token double-colon punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">::</span>Cors <span class="token keyword keyword-do" style="box-sizing: border-box; color: rgb(0, 119, 170);">do</span>
  allow <span class="token keyword keyword-do" style="box-sizing: border-box; color: rgb(0, 119, 170);">do</span>
    origins <span class="token string-literal" style="box-sizing: border-box;"><span class="token string" style="box-sizing: border-box; color: rgb(102, 153, 0);">'*'</span></span>
    resources <span class="token string-literal" style="box-sizing: border-box;"><span class="token string" style="box-sizing: border-box; color: rgb(102, 153, 0);">'*'</span></span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span>
    headers<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:any</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span>
    methods<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> <span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">[</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:get</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:post</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:put</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:patch</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:delete</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:options</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:head</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">]</span>
  <span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span>
<span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span

Rails<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>application<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>config<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>middleware<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>insert_before <span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">0</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> Rack<span class="token double-colon punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">::</span>Cors <span class="token keyword keyword-do" style="box-sizing: border-box; color: rgb(0, 119, 170);">do</span>
  allow <span class="token keyword keyword-do" style="box-sizing: border-box; color: rgb(0, 119, 170);">do</span>
    origins <span class="token string-literal" style="box-sizing: border-box;"><span class="token string" style="box-sizing: border-box; color: rgb(102, 153, 0);">'*'</span></span>
    resources <span class="token string-literal" style="box-sizing: border-box;"><span class="token string" style="box-sizing: border-box; color: rgb(102, 153, 0);">'*'</span></span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span>
    headers<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:any</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span>
    methods<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> <span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">[</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:get</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:post</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:put</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:patch</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:delete</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:options</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:head</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">]</span>
  <span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span>
<span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span

Open Gemfile by scrolling down from the explorer file. Uncomment gem ‘rack-cors’ in line number 26.

<span class="token comment" style="box-sizing: border-box; color: rgb(112, 128, 144);"># in Gemfile</span>
gem <span class="token string-literal" style="box-sizing: border-box;"><span class="token string" style="box-sizing: border-box; color: rgb(102, 153, 0);">'rack-cors'</span></span

<span class="token comment" style="box-sizing: border-box; color: rgb(112, 128, 144);"># in Gemfile</span>
gem <span class="token string-literal" style="box-sizing: border-box;"><span class="token string" style="box-sizing: border-box; color: rgb(102, 153, 0);">'rack-cors'</span></span

<span class="token comment" style="box-sizing: border-box; color: rgb(112, 128, 144);"># in Gemfile</span>
gem <span class="token string-literal" style="box-sizing: border-box;"><span class="token string" style="box-sizing: border-box; color: rgb(102, 153, 0);">'rack-cors'</span></span

Open the terminal and run

$ bundle install
$ bundle install
$ bundle install

Ruby

Step 3: Create a Controller, Model along with a table with
$ rails g resource Post title description
$ rails g resource Post title description
$ rails g resource Post title description

After using the command, it will then generate the below-mentioned files.

Model [Post]:

app <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&gt;</span> models <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&gt;</span> post<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>rb
app <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&gt;</span> models <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&gt;</span> post<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>rb
app <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&gt;</span> models <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&gt;</span> post<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>rb

Controller:

app <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&gt;</span> controllers <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&gt;</span> posts_controller<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>rb
app <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&gt;</span> controllers <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&gt;</span> posts_controller<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>rb
app <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&gt;</span> controllers <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&gt;</span> posts_controller<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>rb

Route:

config <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&gt;</span> routes<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>rb
config <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&gt;</span> routes<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>rb
config <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&gt;</span> routes<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>rb

Database Migration Table:

db <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&gt;</span> migrate <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&gt;</span> <span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">20230127064441</span>_create_posts<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>rb
db <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&gt;</span> migrate <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&gt;</span> <span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">20230127064441</span>_create_posts<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>rb
db <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&gt;</span> migrate <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&gt;</span> <span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">20230127064441</span>_create_posts<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>rb
Step 4: Attributes of Post Model
  • Title of the post

  • Description of the post

Specifying attributesAdd the following Code into db > migrate > 20230127064441_create_posts.rb: 

<span class="token keyword keyword-class" style="box-sizing: border-box; color: rgb(0, 119, 170);">class</span> <span class="token class-name" style="box-sizing: border-box; color: rgb(221, 74, 104);">CreatePosts</span> <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&lt;</span> ActiveRecord<span class="token double-colon punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">::</span>Migration<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">[</span><span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">6.0</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">]</span>
  <span class="token keyword keyword-def" style="box-sizing: border-box; color: rgb(0, 119, 170);">def</span> <span class="token method-definition" style="box-sizing: border-box;"><span class="token function" style="box-sizing: border-box; color: rgb(221, 74, 104);">change</span></span>
    create_table <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:posts</span> <span class="token keyword keyword-do" style="box-sizing: border-box; color: rgb(0, 119, 170);">do</span> <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">|</span>t<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">|</span>
      t<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>string <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:title</span>
      t<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>text <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:description</span>
    <span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span>
  <span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span>
<span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span

<span class="token keyword keyword-class" style="box-sizing: border-box; color: rgb(0, 119, 170);">class</span> <span class="token class-name" style="box-sizing: border-box; color: rgb(221, 74, 104);">CreatePosts</span> <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&lt;</span> ActiveRecord<span class="token double-colon punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">::</span>Migration<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">[</span><span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">6.0</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">]</span>
  <span class="token keyword keyword-def" style="box-sizing: border-box; color: rgb(0, 119, 170);">def</span> <span class="token method-definition" style="box-sizing: border-box;"><span class="token function" style="box-sizing: border-box; color: rgb(221, 74, 104);">change</span></span>
    create_table <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:posts</span> <span class="token keyword keyword-do" style="box-sizing: border-box; color: rgb(0, 119, 170);">do</span> <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">|</span>t<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">|</span>
      t<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>string <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:title</span>
      t<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>text <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:description</span>
    <span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span>
  <span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span>
<span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span

<span class="token keyword keyword-class" style="box-sizing: border-box; color: rgb(0, 119, 170);">class</span> <span class="token class-name" style="box-sizing: border-box; color: rgb(221, 74, 104);">CreatePosts</span> <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&lt;</span> ActiveRecord<span class="token double-colon punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">::</span>Migration<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">[</span><span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">6.0</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">]</span>
  <span class="token keyword keyword-def" style="box-sizing: border-box; color: rgb(0, 119, 170);">def</span> <span class="token method-definition" style="box-sizing: border-box;"><span class="token function" style="box-sizing: border-box; color: rgb(221, 74, 104);">change</span></span>
    create_table <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:posts</span> <span class="token keyword keyword-do" style="box-sizing: border-box; color: rgb(0, 119, 170);">do</span> <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">|</span>t<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">|</span>
      t<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>string <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:title</span>
      t<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>text <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:description</span>
    <span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span>
  <span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span>
<span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span

Migrating the table

$ rails db<span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:migrate</span
$ rails db<span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:migrate</span
$ rails db<span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:migrate</span

If the data has been migrated successfully, then you will be able to see the following:

<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">==</span> <span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">20230127064441</span> CreatePosts<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> migrating <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">===</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">===</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">===</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">===</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">===</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">===</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">===</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">===</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">===</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">==</span>
<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">-</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">-</span> create_table<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:posts</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span>
   <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">-</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&gt;</span> <span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">0.0022</span>s
<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">==</span> <span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">20230127064441</span> CreatePosts<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> migrated <span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span><span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">0.0014</span>s<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span> <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">===</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">===</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">===</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">===</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">===</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">===</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">==</span

<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">==</span> <span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">20230127064441</span> CreatePosts<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> migrating <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">===</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">===</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">===</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">===</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">===</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">===</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">===</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">===</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">===</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">==</span>
<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">-</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">-</span> create_table<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:posts</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span>
   <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">-</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&gt;</span> <span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">0.0022</span>s
<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">==</span> <span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">20230127064441</span> CreatePosts<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> migrated <span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span><span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">0.0014</span>s<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span> <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">===</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">===</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">===</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">===</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">===</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">===</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">==</span

<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">==</span> <span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">20230127064441</span> CreatePosts<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> migrating <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">===</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">===</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">===</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">===</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">===</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">===</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">===</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">===</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">===</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">==</span>
<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">-</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">-</span> create_table<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:posts</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span>
   <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">-</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&gt;</span> <span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">0.0022</span>s
<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">==</span> <span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">20230127064441</span> CreatePosts<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> migrated <span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span><span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">0.0014</span>s<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span> <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">===</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">===</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">===</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">===</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">===</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">===</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">==</span

Step 5: Defining display, index, destroy, update, and create actions.

Here are the implications of the actions:

  • Index: It will display all the posts present in the database.

  • Show: It will display the specific(given as an id) post.

  • Create: It will make the post’s instance.

  • Update: It will update the post-item instance.

  • Delete: It will delete specific post items.

Now copy and then paste the below code in secret_menu_intems_controller.rb.Now let’s write API magic over here.

app <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&gt;</span> controllers <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&gt;</span> posts_controller<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>rb

<span class="token keyword keyword-class" style="box-sizing: border-box; color: rgb(0, 119, 170);">class</span> <span class="token class-name" style="box-sizing: border-box; color: rgb(221, 74, 104);">PostsController</span> <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&lt;</span> ApplicationController
  <span class="token keyword keyword-def" style="box-sizing: border-box; color: rgb(0, 119, 170);">def</span> <span class="token method-definition" style="box-sizing: border-box;"><span class="token function" style="box-sizing: border-box; color: rgb(221, 74, 104);">index</span></span>
    posts <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> Post<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>all
    render json<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> posts<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">status</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> <span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">200</span>
  <span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span>

  <span class="token keyword keyword-def" style="box-sizing: border-box; color: rgb(0, 119, 170);">def</span> <span class="token method-definition" style="box-sizing: border-box;"><span class="token function" style="box-sizing: border-box; color: rgb(221, 74, 104);">show</span></span>
    post <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> post<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>find_by<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">id</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> params<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">[</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:id</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">]</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span>
    <span class="token keyword keyword-if" style="box-sizing: border-box; color: rgb(0, 119, 170);">if</span> post
      render json<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> post<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">status</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> <span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">200</span>
    <span class="token keyword keyword-else" style="box-sizing: border-box; color: rgb(0, 119, 170);">else</span>
      render json<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> <span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">{</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">error</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> “Post Not Found” <span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">}</span>
    <span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span>
  <span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span>

  <span class="token keyword keyword-def" style="box-sizing: border-box; color: rgb(0, 119, 170);">def</span> <span class="token method-definition" style="box-sizing: border-box;"><span class="token function" style="box-sizing: border-box; color: rgb(221, 74, 104);">create</span></span>
    post <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> <span class="token class-name" style="box-sizing: border-box; color: rgb(221, 74, 104);">Post</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span><span class="token keyword keyword-new" style="box-sizing: border-box; color: rgb(0, 119, 170);">new</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span>
             title<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> params<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">[</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:title</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">]</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span>
             description<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> params<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">[</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:description</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">]</span>
           <span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span>
    <span class="token keyword keyword-if" style="box-sizing: border-box; color: rgb(0, 119, 170);">if</span> post<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>save
      render json<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> post
    <span class="token keyword keyword-else" style="box-sizing: border-box; color: rgb(0, 119, 170);">else</span>
      render json<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> <span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">{</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">error</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> “Error on creating a record” <span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">}</span>
    <span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span>
  <span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span>

  <span class="token keyword keyword-def" style="box-sizing: border-box; color: rgb(0, 119, 170);">def</span> <span class="token method-definition" style="box-sizing: border-box;"><span class="token function" style="box-sizing: border-box; color: rgb(221, 74, 104);">update</span></span>
    post <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> Post<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>find_by<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">id</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> params<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">[</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:id</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">]</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span>
    post<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>update<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span>
      title<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> params<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">[</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:title</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">]</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span>
      description<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> params<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">[</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:description</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">]</span>
    <span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span>
    render json<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> <span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">{</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">message</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> “<span class="token comment" style="box-sizing: border-box; color: rgb(112, 128, 144);">#{post.title} has been updated!”, status: 200}</span>
  <span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span>

  <span class="token keyword keyword-def" style="box-sizing: border-box; color: rgb(0, 119, 170);">def</span> <span class="token method-definition" style="box-sizing: border-box;"><span class="token function" style="box-sizing: border-box; color: rgb(221, 74, 104);">destroy</span></span>
    post <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> Post<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>find_by<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">id</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> params<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">[</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:id</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">]</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span>
    <span class="token keyword keyword-if" style="box-sizing: border-box; color: rgb(0, 119, 170);">if</span> post
      post<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>destroy
      render json<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> “<span class="token comment" style="box-sizing: border-box; color: rgb(112, 128, 144);">#{post.title} has been deleted!”</span>
    <span class="token keyword keyword-else" style="box-sizing: border-box; color: rgb(0, 119, 170);">else</span>
      render json<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> <span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">{</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">error</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> “Post Not Found” <span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">}</span>
    <span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span>
  <span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span>
<span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span

app <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&gt;</span> controllers <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&gt;</span> posts_controller<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>rb

<span class="token keyword keyword-class" style="box-sizing: border-box; color: rgb(0, 119, 170);">class</span> <span class="token class-name" style="box-sizing: border-box; color: rgb(221, 74, 104);">PostsController</span> <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&lt;</span> ApplicationController
  <span class="token keyword keyword-def" style="box-sizing: border-box; color: rgb(0, 119, 170);">def</span> <span class="token method-definition" style="box-sizing: border-box;"><span class="token function" style="box-sizing: border-box; color: rgb(221, 74, 104);">index</span></span>
    posts <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> Post<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>all
    render json<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> posts<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">status</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> <span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">200</span>
  <span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span>

  <span class="token keyword keyword-def" style="box-sizing: border-box; color: rgb(0, 119, 170);">def</span> <span class="token method-definition" style="box-sizing: border-box;"><span class="token function" style="box-sizing: border-box; color: rgb(221, 74, 104);">show</span></span>
    post <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> post<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>find_by<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">id</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> params<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">[</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:id</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">]</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span>
    <span class="token keyword keyword-if" style="box-sizing: border-box; color: rgb(0, 119, 170);">if</span> post
      render json<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> post<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">status</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> <span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">200</span>
    <span class="token keyword keyword-else" style="box-sizing: border-box; color: rgb(0, 119, 170);">else</span>
      render json<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> <span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">{</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">error</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> “Post Not Found” <span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">}</span>
    <span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span>
  <span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span>

  <span class="token keyword keyword-def" style="box-sizing: border-box; color: rgb(0, 119, 170);">def</span> <span class="token method-definition" style="box-sizing: border-box;"><span class="token function" style="box-sizing: border-box; color: rgb(221, 74, 104);">create</span></span>
    post <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> <span class="token class-name" style="box-sizing: border-box; color: rgb(221, 74, 104);">Post</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span><span class="token keyword keyword-new" style="box-sizing: border-box; color: rgb(0, 119, 170);">new</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span>
             title<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> params<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">[</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:title</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">]</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span>
             description<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> params<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">[</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:description</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">]</span>
           <span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span>
    <span class="token keyword keyword-if" style="box-sizing: border-box; color: rgb(0, 119, 170);">if</span> post<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>save
      render json<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> post
    <span class="token keyword keyword-else" style="box-sizing: border-box; color: rgb(0, 119, 170);">else</span>
      render json<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> <span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">{</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">error</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> “Error on creating a record” <span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">}</span>
    <span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span>
  <span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span>

  <span class="token keyword keyword-def" style="box-sizing: border-box; color: rgb(0, 119, 170);">def</span> <span class="token method-definition" style="box-sizing: border-box;"><span class="token function" style="box-sizing: border-box; color: rgb(221, 74, 104);">update</span></span>
    post <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> Post<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>find_by<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">id</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> params<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">[</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:id</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">]</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span>
    post<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>update<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span>
      title<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> params<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">[</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:title</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">]</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span>
      description<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> params<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">[</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:description</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">]</span>
    <span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span>
    render json<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> <span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">{</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">message</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> “<span class="token comment" style="box-sizing: border-box; color: rgb(112, 128, 144);">#{post.title} has been updated!”, status: 200}</span>
  <span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span>

  <span class="token keyword keyword-def" style="box-sizing: border-box; color: rgb(0, 119, 170);">def</span> <span class="token method-definition" style="box-sizing: border-box;"><span class="token function" style="box-sizing: border-box; color: rgb(221, 74, 104);">destroy</span></span>
    post <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> Post<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>find_by<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">id</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> params<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">[</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:id</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">]</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span>
    <span class="token keyword keyword-if" style="box-sizing: border-box; color: rgb(0, 119, 170);">if</span> post
      post<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>destroy
      render json<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> “<span class="token comment" style="box-sizing: border-box; color: rgb(112, 128, 144);">#{post.title} has been deleted!”</span>
    <span class="token keyword keyword-else" style="box-sizing: border-box; color: rgb(0, 119, 170);">else</span>
      render json<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> <span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">{</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">error</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> “Post Not Found” <span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">}</span>
    <span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span>
  <span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span>
<span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span

app <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&gt;</span> controllers <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&gt;</span> posts_controller<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>rb

<span class="token keyword keyword-class" style="box-sizing: border-box; color: rgb(0, 119, 170);">class</span> <span class="token class-name" style="box-sizing: border-box; color: rgb(221, 74, 104);">PostsController</span> <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&lt;</span> ApplicationController
  <span class="token keyword keyword-def" style="box-sizing: border-box; color: rgb(0, 119, 170);">def</span> <span class="token method-definition" style="box-sizing: border-box;"><span class="token function" style="box-sizing: border-box; color: rgb(221, 74, 104);">index</span></span>
    posts <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> Post<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>all
    render json<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> posts<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">status</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> <span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">200</span>
  <span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span>

  <span class="token keyword keyword-def" style="box-sizing: border-box; color: rgb(0, 119, 170);">def</span> <span class="token method-definition" style="box-sizing: border-box;"><span class="token function" style="box-sizing: border-box; color: rgb(221, 74, 104);">show</span></span>
    post <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> post<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>find_by<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">id</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> params<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">[</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:id</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">]</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span>
    <span class="token keyword keyword-if" style="box-sizing: border-box; color: rgb(0, 119, 170);">if</span> post
      render json<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> post<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">status</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> <span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">200</span>
    <span class="token keyword keyword-else" style="box-sizing: border-box; color: rgb(0, 119, 170);">else</span>
      render json<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> <span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">{</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">error</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> “Post Not Found” <span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">}</span>
    <span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span>
  <span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span>

  <span class="token keyword keyword-def" style="box-sizing: border-box; color: rgb(0, 119, 170);">def</span> <span class="token method-definition" style="box-sizing: border-box;"><span class="token function" style="box-sizing: border-box; color: rgb(221, 74, 104);">create</span></span>
    post <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> <span class="token class-name" style="box-sizing: border-box; color: rgb(221, 74, 104);">Post</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span><span class="token keyword keyword-new" style="box-sizing: border-box; color: rgb(0, 119, 170);">new</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span>
             title<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> params<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">[</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:title</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">]</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span>
             description<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> params<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">[</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:description</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">]</span>
           <span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span>
    <span class="token keyword keyword-if" style="box-sizing: border-box; color: rgb(0, 119, 170);">if</span> post<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>save
      render json<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> post
    <span class="token keyword keyword-else" style="box-sizing: border-box; color: rgb(0, 119, 170);">else</span>
      render json<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> <span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">{</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">error</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> “Error on creating a record” <span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">}</span>
    <span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span>
  <span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span>

  <span class="token keyword keyword-def" style="box-sizing: border-box; color: rgb(0, 119, 170);">def</span> <span class="token method-definition" style="box-sizing: border-box;"><span class="token function" style="box-sizing: border-box; color: rgb(221, 74, 104);">update</span></span>
    post <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> Post<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>find_by<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">id</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> params<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">[</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:id</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">]</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span>
    post<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>update<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span>
      title<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> params<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">[</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:title</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">]</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span>
      description<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> params<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">[</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:description</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">]</span>
    <span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span>
    render json<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> <span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">{</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">message</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> “<span class="token comment" style="box-sizing: border-box; color: rgb(112, 128, 144);">#{post.title} has been updated!”, status: 200}</span>
  <span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span>

  <span class="token keyword keyword-def" style="box-sizing: border-box; color: rgb(0, 119, 170);">def</span> <span class="token method-definition" style="box-sizing: border-box;"><span class="token function" style="box-sizing: border-box; color: rgb(221, 74, 104);">destroy</span></span>
    post <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> Post<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>find_by<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">id</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> params<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">[</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:id</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">]</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span>
    <span class="token keyword keyword-if" style="box-sizing: border-box; color: rgb(0, 119, 170);">if</span> post
      post<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>destroy
      render json<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> “<span class="token comment" style="box-sizing: border-box; color: rgb(112, 128, 144);">#{post.title} has been deleted!”</span>
    <span class="token keyword keyword-else" style="box-sizing: border-box; color: rgb(0, 119, 170);">else</span>
      render json<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> <span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">{</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">error</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> “Post Not Found” <span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">}</span>
    <span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span>
  <span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span>
<span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span

Step 6: Creating routes for index, create, show, delete, and update actions.

Routes receive HTTP requests that come from the client side. We have to forward it using the correct actions. To configure the route, copy the following code and paste it into the route.rb.

<span class="token comment" style="box-sizing: border-box; color: rgb(112, 128, 144);"># config &gt; routes.rb</span>

Rails<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>application<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>routes<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>draw <span class="token keyword keyword-do" style="box-sizing: border-box; color: rgb(0, 119, 170);">do</span>
  resources <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:posts</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">only</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> <span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">[</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:index</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:show</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:create</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:update</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:destroy</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">]</span>
<span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span

<span class="token comment" style="box-sizing: border-box; color: rgb(112, 128, 144);"># config &gt; routes.rb</span>

Rails<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>application<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>routes<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>draw <span class="token keyword keyword-do" style="box-sizing: border-box; color: rgb(0, 119, 170);">do</span>
  resources <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:posts</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">only</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> <span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">[</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:index</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:show</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:create</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:update</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:destroy</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">]</span>
<span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span

<span class="token comment" style="box-sizing: border-box; color: rgb(112, 128, 144);"># config &gt; routes.rb</span>

Rails<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>application<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>routes<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>draw <span class="token keyword keyword-do" style="box-sizing: border-box; color: rgb(0, 119, 170);">do</span>
  resources <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:posts</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">only</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> <span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">[</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:index</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:show</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:create</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:update</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:destroy</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">]</span>
<span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span

Step 7: Seeding the data

Now in the database, create secret menu item instances.

<span class="token comment" style="box-sizing: border-box; color: rgb(112, 128, 144);"># db &gt; seed.rb</span>

post1 <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> Post<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>create<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">title</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> <span class="token string-literal" style="box-sizing: border-box;"><span class="token string" style="box-sizing: border-box; color: rgb(102, 153, 0);">"Better way to improve Ruby on Rails coding"</span></span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">description</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:"Lorem Ipsum is simply dummy text of the printing and typesetting industry."</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span>

post2 <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> Post<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>create<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">title</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> <span class="token string-literal" style="box-sizing: border-box;"><span class="token string" style="box-sizing: border-box; color: rgb(102, 153, 0);">"perfect Combination of Angular + Ruby on Rails"</span></span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">menu_description</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:"Lorem Ipsum is simply dummy text of the printing and typesetting industry."</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span

<span class="token comment" style="box-sizing: border-box; color: rgb(112, 128, 144);"># db &gt; seed.rb</span>

post1 <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> Post<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>create<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">title</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> <span class="token string-literal" style="box-sizing: border-box;"><span class="token string" style="box-sizing: border-box; color: rgb(102, 153, 0);">"Better way to improve Ruby on Rails coding"</span></span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">description</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:"Lorem Ipsum is simply dummy text of the printing and typesetting industry."</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span>

post2 <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> Post<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>create<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">title</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> <span class="token string-literal" style="box-sizing: border-box;"><span class="token string" style="box-sizing: border-box; color: rgb(102, 153, 0);">"perfect Combination of Angular + Ruby on Rails"</span></span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">menu_description</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:"Lorem Ipsum is simply dummy text of the printing and typesetting industry."</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span

<span class="token comment" style="box-sizing: border-box; color: rgb(112, 128, 144);"># db &gt; seed.rb</span>

post1 <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> Post<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>create<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">title</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> <span class="token string-literal" style="box-sizing: border-box;"><span class="token string" style="box-sizing: border-box; color: rgb(102, 153, 0);">"Better way to improve Ruby on Rails coding"</span></span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">description</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:"Lorem Ipsum is simply dummy text of the printing and typesetting industry."</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span>

post2 <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> Post<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>create<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">title</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> <span class="token string-literal" style="box-sizing: border-box;"><span class="token string" style="box-sizing: border-box; color: rgb(102, 153, 0);">"perfect Combination of Angular + Ruby on Rails"</span></span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">menu_description</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:"Lorem Ipsum is simply dummy text of the printing and typesetting industry."</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span

Seeding the data
$ rails db<span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:seed</span
$ rails db<span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:seed</span
$ rails db<span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:seed</span

Verify if seeding was correctly done:

$ rails c
$ rails c
$ rails c

# It will now show a console

<span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">2.6</span><span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">.1</span> <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span><span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">002</span> <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&gt;</span
<span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">2.6</span><span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">.1</span> <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span><span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">002</span> <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&gt;</span
<span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">2.6</span><span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">.1</span> <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span><span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">002</span> <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&gt;</span

Now you can extract all the instances of secret menu items by typing SecretMenuItem.all

<span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">2.6</span><span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">.1</span> <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span><span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">002</span> <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&gt;</span> Post<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>all
Post Load <span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span><span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">0.1</span>ms<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span> <span class="token constant" style="box-sizing: border-box; color: rgb(153, 0, 85);">SELECT</span> <span class="token string-literal" style="box-sizing: border-box;"><span class="token string" style="box-sizing: border-box; color: rgb(102, 153, 0);">"secret_menu_items"</span></span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">*</span> <span class="token constant" style="box-sizing: border-box; color: rgb(153, 0, 85);">FROM</span> <span class="token string-literal" style="box-sizing: border-box;"><span class="token string" style="box-sizing: border-box; color: rgb(102, 153, 0);">"posts"</span></span>

 <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=&gt;</span> <span class="token comment" style="box-sizing: border-box; color: rgb(112, 128, 144);">#&lt;ActiveRecord::Relation [#&lt;Post id: 1, title: "Better way to improve Ruby on Rails coding", description:"Lorem Ipsum is simply dummy text of the printing and typesetting industry."&gt;, #&lt; Post id: 2, title: "perfect Combination of Angular + Ruby on Rails", menu_description:"Lorem Ipsum is simply dummy text of the printing and typesetting industry."&gt;]&gt;</span>
<span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">2.6</span><span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">.1</span> <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span><span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">002</span> <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&gt;</span> Post<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>all
Post Load <span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span><span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">0.1</span>ms<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span> <span class="token constant" style="box-sizing: border-box; color: rgb(153, 0, 85);">SELECT</span> <span class="token string-literal" style="box-sizing: border-box;"><span class="token string" style="box-sizing: border-box; color: rgb(102, 153, 0);">"secret_menu_items"</span></span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">*</span> <span class="token constant" style="box-sizing: border-box; color: rgb(153, 0, 85);">FROM</span> <span class="token string-literal" style="box-sizing: border-box;"><span class="token string" style="box-sizing: border-box; color: rgb(102, 153, 0);">"posts"</span></span>

 <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=&gt;</span> <span class="token comment" style="box-sizing: border-box; color: rgb(112, 128, 144);">#&lt;ActiveRecord::Relation [#&lt;Post id: 1, title: "Better way to improve Ruby on Rails coding", description:"Lorem Ipsum is simply dummy text of the printing and typesetting industry."&gt;, #&lt; Post id: 2, title: "perfect Combination of Angular + Ruby on Rails", menu_description:"Lorem Ipsum is simply dummy text of the printing and typesetting industry."&gt;]&gt;</span>
<span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">2.6</span><span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">.1</span> <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span><span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">002</span> <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&gt;</span> Post<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>all
Post Load <span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span><span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">0.1</span>ms<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span> <span class="token constant" style="box-sizing: border-box; color: rgb(153, 0, 85);">SELECT</span> <span class="token string-literal" style="box-sizing: border-box;"><span class="token string" style="box-sizing: border-box; color: rgb(102, 153, 0);">"secret_menu_items"</span></span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">*</span> <span class="token constant" style="box-sizing: border-box; color: rgb(153, 0, 85);">FROM</span> <span class="token string-literal" style="box-sizing: border-box;"><span class="token string" style="box-sizing: border-box; color: rgb(102, 153, 0);">"posts"</span></span>

 <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=&gt;</span> <span class="token comment" style="box-sizing: border-box; color: rgb(112, 128, 144);">#&lt;ActiveRecord::Relation [#&lt;Post id: 1, title: "Better way to improve Ruby on Rails coding", description:"Lorem Ipsum is simply dummy text of the printing and typesetting industry."&gt;, #&lt; Post id: 2, title: "perfect Combination of Angular + Ruby on Rails", menu_description:"Lorem Ipsum is simply dummy text of the printing and typesetting industry."&gt;]&gt;</span>

If all the instances are visible, then the seeding was done correctly.

Check Your Magic

– Start rails server.

– Go to your preferred browser(Or you can check into POSTMAN also).

– Pass the following line into the browser URL

http://localhost:3000/posts

Conclusion

The digital world is becoming more and more API-driven, and API development is the prime need for effective and faster digitization. Developing APIs in Ruby on Rails is the first choice for businesses as it not just creates faster APIs but also in a secure and scalable way.

Being one of the top Ruby on Rails development company, our RoR developers develops APIs for our customers from various industry segments.

Feel free to get in touch with us to explore new ways and possibilities with API and RoR Development.

Similar Blogs

Explore Similar Blogs

Explore Similar Blogs

Learn how AI is transforming meetings, notes,
and team productivity.

Learn how AI is transforming meetings, notes,
and team productivity.

next step

Tell us what you are trying to build.

No spec required. Send a paragraph, an old repo link, or a screenshot of the system you are replacing. A senior Rails engineer will review it and reply with real feedback.

essence logo

next step

Tell us what you are trying to build.

No spec required. Send a paragraph, an old repo link, or a screenshot of the system you are replacing. A senior Rails engineer will review it and reply with real feedback.

essence logo

next step

Tell us what you are trying to build.

No spec required. Send a paragraph, an old repo link, or a screenshot of the system you are replacing. A senior Rails engineer will review it and reply with real feedback.

essence logo