Skip to content

Commit f3bd413

Browse files
authored
Update README.md
1 parent 94088eb commit f3bd413

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,37 @@ Let's begin!
116116
3. Run ``` npm start ``` and type Y in case there are some conflicting port configs in your local host. You should see the bottom image in your browser if everything was installed correctly!
117117

118118
<a href="http://tinypic.com?ref=2gwysso" target="_blank"><img src="http://i68.tinypic.com/2gwysso.png" border="0" alt="Image and video hosting by TinyPic"></a>
119+
120+
## Rails API 💎
121+
122+
Now that have our virtual enviorment ready, we can create our first ever Rails API. The new rails api command scaffolds everything we need to get up and ready for our project. Let's start our vagrant server and ssh into our project folder.
123+
124+
1. Run the following: ``` rails new my-first-api --api -T ```
125+
126+
What's going on here? The ```--api``` command tells rails that we want an API structure application instead of a standard rails structure. The ``` -T ``` command also tells rails that we don't want Minitest as our testing suite. You'll most likely be used to Rspec so we'll talk about that later in the guide.
127+
128+
2. Enable Cross-Origin Resource Sharing (CORS) in your gem and config directory. Locate your gemfile and uncomment the following
129+
```ruby
130+
# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible
131+
gem 'rack-cors'
132+
```
133+
134+
Do not forget to ```bundle install``` !
135+
136+
Now in your config/initializers directory, you should now see a ```cors.rb``` file. Add the following to
137+
138+
```ruby
139+
# config/initializers/cors.rb
140+
class Application < Rails::Application
141+
142+
config.middleware.insert_before 0, "Rack::Cors" do
143+
allow do
144+
origins '*'
145+
resource '*', :headers => :any, :methods => [:get, :post, :patch, :options]
146+
end
147+
end
148+
149+
end
150+
```
151+
152+
Since this tutorial is mainly for testing and toy projects, we are allowing ALL methods from another domain. You should tailor the header and methods to your liking.

0 commit comments

Comments
 (0)