{"id":15152572,"url":"https://github.com/thamerh/ruby-on-rails","last_synced_at":"2026-02-05T09:32:12.231Z","repository":{"id":244394017,"uuid":"815117111","full_name":"thamerh/ruby-on-rails","owner":"thamerh","description":"This Markdown file is a comprehensive guide to setting up and using Ruby on Rails for API development. It covers installation, database configuration, model and controller generation, routing, seeding, API testing, CORS configuration for frontend connectivity, and basic Ruby syntax and variable declarations for beginners.","archived":false,"fork":false,"pushed_at":"2024-06-14T11:47:41.000Z","size":2,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-04T07:41:58.322Z","etag":null,"topics":["api","crud","ruby","ruby-on-rails"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thamerh.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-06-14T11:43:16.000Z","updated_at":"2024-06-14T11:51:22.000Z","dependencies_parsed_at":"2024-06-14T13:04:05.956Z","dependency_job_id":"0ac48bd2-4c8b-489d-b9ba-1aa5fbe4f4e6","html_url":"https://github.com/thamerh/ruby-on-rails","commit_stats":null,"previous_names":["thamerh/ruby-on-rails"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/thamerh/ruby-on-rails","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thamerh%2Fruby-on-rails","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thamerh%2Fruby-on-rails/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thamerh%2Fruby-on-rails/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thamerh%2Fruby-on-rails/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thamerh","download_url":"https://codeload.github.com/thamerh/ruby-on-rails/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thamerh%2Fruby-on-rails/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266801331,"owners_count":23986370,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-24T02:00:09.469Z","response_time":99,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","crud","ruby","ruby-on-rails"],"created_at":"2024-09-26T16:03:49.767Z","updated_at":"2026-02-05T09:32:12.194Z","avatar_url":"https://github.com/thamerh.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Complete Guide to Learning Ruby on Rails\n\n## Basic Ruby Syntax and Variable Declaration\n\n```ruby\n# Variable declaration\nvariable_name = \"value\"\nnumber_variable = 10\narray_variable = [1, 2, 3]\nhash_variable = { key: \"value\", another_key: \"another value\" }\n\n# Conditional statement\nif condition\n  # code to execute if condition is true\nelse\n  # code to execute if condition is false\nend\n\n# Looping (example of each loop)\narray_variable.each do |element|\n  puts element\nend\n\n# Method definition\ndef method_name(parameter1, parameter2)\n  # method body\nend\n\n# Class definition\nclass ClassName\n  def initialize(parameter)\n    @parameter = parameter\n  end\n\n  def instance_method\n    # method body\n  end\nend\n```\n\n## Installing Ruby on Rails\n\n```bash\n# Install Bundler\n$ gem install bundler\n\n# Install Rails\n$ gem install rails\n\n# Install all required gems specified in the Gemfile\n$ bundle install\n```\n\n## Creating a New Rails Application\n\n```bash\n# Create a new Rails application configured for API development\n$ rails new Example -d postgresql --api\n$ cd Example\n```\n\n## Configuring the Database\n\n```yaml\n# PostgreSQL configuration for Rails\ndefault: \u0026default\n  adapter: postgresql\n  username: username\n  password: password\n  host: localhost\n  pool: \u003c%= ENV.fetch(\"RAILS_MAX_THREADS\") { 5 } %\u003e\n  timeout: 5000\n\ndevelopment:\n  \u003c\u003c: *default\n  database: example_development\n\ntest:\n  \u003c\u003c: *default\n  database: example_test\n\nproduction:\n  \u003c\u003c: *default\n  database: example_production\n```\n\n## Generating Models and Migrating the Database\n\n```bash\n# Create a model\n$ rails g model ModelName key:type key1:type1 key2:type2\n\n# Create the database\n$ rails db:create\n\n# Run migrations\n$ rails db:migrate\n```\n\n## Generating Controllers\n\n```bash\n# Create a controller (auto-generate controller name)\n$ rails g controller api/v1/ControllerName\n```\n\n## Defining Routes\n\n```ruby\nRails.application.routes.draw do\n  namespace :api do\n    namespace :v1 do\n      resources :controller_name\n    end\n  end\nend\n```\n\n## Creating Controller Actions\n\n```ruby\nclass Api::V1::ControllerNameController \u003c ApplicationController\n  before_action :set_model_name, only: [:show, :update, :destroy]\n\n  def index\n    @data = ModelName.all\n    render json: @data\n  end\n\n  def show\n    render json: @model_name\n  end\n\n  def create\n    @model_instance = ModelName.new(model_name_params)\n    if @model_instance.save\n      render json: @model_instance, status: :created\n    else\n      render json: @model_instance.errors, status: :unprocessable_entity\n    end\n  end\n\n  def update\n    if @model_name.update(model_name_params)\n      render json: @model_name\n    else\n      render json: @model_name.errors, status: :unprocessable_entity\n    end\n  end\n\n  def destroy\n    @model_name.destroy\n    head :no_content\n  end\n\n  private\n\n  def set_model_name\n    @model_name = ModelName.find_by(id: params[:id])\n    render json: { error: 'ModelName not found' }, status: :not_found unless @model_name\n  end\n\n  def model_name_params\n    params.require(:model_name).permit(:attribute1, :attribute2)\n  end\nend\n```\n\n## Seeding the Database\n\n```ruby\n# db/seeds.rb\nModelName.create!(key1: \"example\", value1: \"example\")\nModelName.create!(key2: \"example\", value2: \"example\")\nModelName.create!(key3: \"example\", value3: \"example\")\n```\n\n```bash\n# Run the seed to populate the database\n$ rails db:seed\n```\n\n## Testing the API\n\n### Making Requests\n\n- **GET**: Set the request type to \"GET\" and enter your API endpoint URL:\n  `http://localhost:port/api/v1/controller_name`\n\n- **POST**: Set the request type to \"POST\" and use the same API endpoint URL. In the \"Body\" tab, select \"raw,\" and choose \"JSON (application/json)\" from the dropdown.\n\n- **PUT**: Set the request type to \"PUT\" and use the endpoint URL with the record's ID:\n  `http://localhost:port/api/v1/controller_name/:id` (replace `:id` with the actual record ID). In the \"Body\" tab, choose \"raw\" and \"JSON (application/json)\".\n\n- **DELETE**: Set the request type to \"DELETE\" and use the endpoint URL with the record's ID:\n  `http://localhost:port/api/v1/controller_name/:id`\n\n## Connecting Frontend to Backend\n\n```ruby\n# Gemfile\ngem 'rack-cors'\n```\n\n```bash\n# Install the gem via the terminal\n$ bundle install\n```\n\n```ruby\n# config/initializers/cors.rb\nRails.application.config.middleware.insert_before 0, Rack::Cors do\n  allow do\n    origins \"http://localhost:PORT\"\n\n    resource \"*\",\n      headers: :any,\n      methods: [:get, :post, :put, :patch, :delete, :options, :head]\n  end\nend\n```\n\nThis Markdown file provides a complete syntax-highlighted guide to setting up and using Ruby on Rails for API development, covering installation, database configuration, model and controller generation, routing, seeding, API testing, configuring CORS for frontend connectivity, and includes basic Ruby syntax and variable declarations for beginners.\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthamerh%2Fruby-on-rails","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthamerh%2Fruby-on-rails","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthamerh%2Fruby-on-rails/lists"}