Build Rails development environment

Even for experienced developers, it takes several twists and turns to install Ruby, Rails, and related software.

These problems are caused by the diversity of the environment. Different operating systems, version numbers, text editors, etc. will lead to different environments.

For developers with Rails development experience, most of them will choose macOS system for Rails development, but macOS system is relatively small and mac computer price is relatively expensive. For many developers who want to learn Rails, this is a problem that can not be ignored.

With the continuous upgrading of windows system, Rails development can now be carried out on Windows system with the help of WSL.

Local development environment (Windows+WSL+Ubuntu)

It's better to have a mac.

Install basic software

sudo apt-get install sqlite3  libsqlite3-dev gcc g++ make git libssl-dev libreadline-dev zlib1g-dev  --fix-missing

Install Ruby

# Installing rbenv
bash -c "$(curl -fsSL https://gitee.com/RubyKids/rbenv-cn/raw/master/install.sh)"

# ruby source acceleration
git clone git://github.com/AndorChen/rbenv-china-mirror.git ~/.rbenv/plugins/rbenv-china-mirror

# Install basic software
sudo apt-get install -y libssl-dev sqlite3 libsqlite3-dev

# Install ruby
rbenv install 3.2.0-dev

Install node js

bash -c "$(curl -fsSL https://gitee.com/RubyKids/nvm-cn/raw/master/install.sh)"

nvm install 14

gem source acceleration

gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.org/

Installing Rails

gem install rails -v 7.0.2

First Rails application

Rails applications generally start with the rails new command, which is used to quickly create rails applications.

Create a new directory for the Rails project, for example

cd ~ # Return to the root directory of the current user
mkdir learn_rails
cd learn_rails

Execute the rails new command

rails new hello_app

After creating a new Rails application, the next step is to use the bundle command to install the gem required by the application.

cd ~/learn_rails/hello_app
bundle install

The speed of installing gem with bundler may not be ideal. At this time, you can use the domestic image to speed up:

  bundle config mirror.https://rubygems.org https://gems.ruby-china.com

Launch Rails app

rails s

Open in browser: http://127.0.0.1:3000/

As shown in the figure below:

[the transfer of external chain pictures fails. The source station may have anti-theft chain mechanism. It is recommended to save the pictures and upload them directly (img-2Xh5m4Wg-1645109537169)(img/1.jpg)]

Installing RailsPanel

Rails panel is a Chrome extension developed for rails.

Install plug-ins: install

If you encounter network problems, Download installation package , and then unzip it. Click on the extension program interface of Chrome to load the unzipped extension program
Button.

To use this extension, you need to add meta_ Add request gem to the Gemfile of the application:

group :development do
  gem 'meta_request'
  # If you are using rails 7, please add it as follows
  gem 'meta_request', git: 'git@gitee.com:rails-train-camp02/meta_request.git'
end

Add resources using scaffolding

rails generate scaffold user name:string email:string
rails db:migrate
rails s

Then open it in the browser http://127.0.0.1:3000/users , and open the rails panel.

As shown in the figure below:

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-eU6l9zsw-1645109537170)(img/2.jpg)]

With the blessing of railpanel, you can speed up the speed of learning Rails. It is recommended to configure it. It is very simple and does not affect the generation environment.

epilogue

The construction of Rails development environment is relatively simple.

Keywords: Ruby git macOS

Added by djfox on Thu, 17 Feb 2022 23:58:46 +0200