The ruby code is inside the friend-list-on-rails
directory.
The docker file & scripts are a docker-compose wrapper so I have a clean, portable, stable environment to work with.
A quick crash course to get me clued into Ruby on Rails before digging into it more. Based on the video "Learn Ruby on Rails - Full Course" by freeCodeCamp.org.
Based on https://docs.docker.com/samples/rails/. Requires:
docker
(20.10.7)docker-compose
(1.28.0)bash
(4.4.2)
- Run
./run.sh dev
to start container in development mode.- If you wish to rebuild the images, run
./run.sh dev --build
- If you wish to rebuild the images, run
- Run
./dev.sh
to start interactive session.su developer
to become your HOST user (essential for permissions)entrypoint.sh
serves Ruby by default.
- Access at
http://localhost:3000/
. - To stop
CTRL + C
ordocker-compose down
.
You may need to run sudo chown -R $USER:$USER *
to fix permissions if you create files within the container as root.
Ruby
- https://marketplace.visualstudio.com/items?itemName=rebornix.RubyRuby Solargraph
- https://marketplace.visualstudio.com/items?itemName=castwide.solargraph
rails s
- Starts rails server.rails routes
- Display all the routes.erb
- Embeded Ruby.rails g controller home index
- Generators creates controller from template. Can be found athttp://localhost:3000/home/index
.- Start partials with
_
such as_header.html.erb
then you can call<%= render 'home/header' %>
- Links work as:
<%= link_to 'About Us', home_about_path, class:"nav-link" %>
where the paths are like your routes/controller;home/about
->home_about
then_path
. You can get this info fromrails routes
$ rails routes | grep home_about home_about GET /home/about(.:format) home#about
rails g scaffold friend first_name:string last_name:string email:string phone:string twitter:string
- Needs to be singular or will be forced singular.
- Review: https://guides.rubyonrails.org/v3.2/migrations.html#supported-types
rails db:migrate
- Runs the migrations.== 20210627175159 CreateFriends: migrating ==================================== -- create_table(:friends) -> 0.0055s == 20210627175159 CreateFriends: migrated (0.0057s) ===========================
resources :friends
- Handles a bunch of sub-routes.- https://rubygems.org/ - Normally:
- Copy paste into
Gemfile
(gem 'devise', '~> 4.8'
) bundle install
(Need to be root in container)- https://rubygems.org/gems/devise has more steps
rails g devise:install
Running via Spring preloader in process 369 create config/initializers/devise.rb create config/locales/devise.en.yml =============================================================================== Depending on your application's configuration some manual setup may be required: 1. Ensure you have defined default url options in your environments files. Here is an example of default_url_options appropriate for a development environment in config/environments/development.rb: config.action_mailer.default_url_options = { host: 'localhost', port: 3000 } In production, :host should be set to the actual host of your application. * Required for all applications. * 2. Ensure you have defined root_url to *something* in your config/routes.rb. For example: root to: "home#index" * Not required for API-only Applications * 3. Ensure you have flash messages in app/views/layouts/application.html.erb. For example: <p class="notice"><%= notice %></p> <p class="alert"><%= alert %></p> * Not required for API-only Applications * 4. You can copy Devise views (for customization) to your app by running: rails g devise:views * Not required * ===============================================================================
rails g devise:views
rails g devise user
+rails db:migrate
- Copy paste into
- https://guides.rubyonrails.org/association_basics.html
- Associations are fairly similar to PHP Akelos Framework framework, which makes sense since Akelos was intended to be a Rail port to PHP.
rails g migration add_user_id_to_friends user_id:integer:index
&&rails db:migrate
- Much like Akelos, you can assign foreign key values in the controller like:
@friend.user_id = current_user.id
- Alternatively:
@friend = current_user.friends.build(friend_params)
friend_params
defines the params for the controller.before_action
is very similar to Akelos.
<%= @friend.inspect =>
or indeed any object like<%= object.inspect =>
lets you see the values.- If you want to only generate the scaffold when the model exists:
rails g scaffold_controller friend_example
- Next?
Since I'm developing in Docker, if I decide to do this again:
To bootstrap without Gemfile, you need to disable some steps in entrypoint/Dockerfile/docker-compose.
Then run rails new friend-list-on-rails
.
When setting up the database for the first time need to run rake db:create
.