Code Monkey home page Code Monkey logo

Comments (7)

ahcarpenter avatar ahcarpenter commented on July 30, 2024

@iamkdev try adding this code to application.rb:

config.middleware.insert_before 3, 'Rack::Cors', debug: true, logger: (-> { Rails.logger }) do
  allow do
    origins ENV['CLIENT_ROOT_URL'] # in your case would want equal to http://localhost:8000

    resource '/api/*',
      headers: :any,
      methods: %i[get post delete put patch options head]
  end
end

from rails-realworld-example-app.

a45b avatar a45b commented on July 30, 2024

Thanks @ahcarpenter. It works.

from rails-realworld-example-app.

ahcarpenter avatar ahcarpenter commented on July 30, 2024

@iamkdev welcome!

from rails-realworld-example-app.

rajeevkannav avatar rajeevkannav commented on July 30, 2024

image

Seems it's not fixed. I checkedout to Rails 5.1 Branch and It does'nt working.

from rails-realworld-example-app.

rajeevkannav avatar rajeevkannav commented on July 30, 2024

As per above Comment origins ENV['CLIENT_ROOT_URL'] reference which is now

origins Rails.application.secrets[:client_root_url] in environment.

To make it working One should update client_root_url in secrets YAML depending upon React/FrontEnd App Port.

For Example, if my React/FrontEnd app Port is 4000 then client_root_url should be http://localhost:4100

Please consider this issue Closed (as it already). This comment is for Other People like me.

from rails-realworld-example-app.

a45b avatar a45b commented on July 30, 2024

Thanks @rajeevkannav.

from rails-realworld-example-app.

hyperrjas avatar hyperrjas commented on July 30, 2024

I'm still having the same problem with localhost an angular4

environment.ts

export const environment = {
  production: false,
  api_url: 'http://localhost:4201/api'
};

secrets.yml

shared:
  client_root_url: http://localhost:4201

application.rb

# frozen_string_literal: true

require_relative 'boot'

require 'rails/all'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module Conduit
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 5.1
    config.api_only = true

    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.
    config.middleware.insert_before 0, Rack::Cors, debug: true, logger: (-> { Rails.logger }) do
      allow do
        origins Rails.application.secrets[:client_root_url]

        resource '/api/*',
                 headers: :any,
                 methods: %i[get post delete put patch options head]
      end
    end
  end
end

rails server
rails s -p 4201

Thank you!

from rails-realworld-example-app.

Related Issues (10)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.