Tutorial - Steveng426/Crud-Ruby-On-Rails GitHub Wiki

CREATE A WEB APPLICATION WITH RUBY ON RAILS as prerequisites you need to have installed ruby. Here I will leave the link for you to download http://railsinstaller.org/en 1 in my case, use version 2.3.3 for Windows

we will proceed to open our IDE in this occasion to use visual studio code

  1. Open visual studio code or the IDE with which you will work 2

  2. In visual studio code we open a new terminal. we go to the menu bar we give in Terminal then in new terminal or with the shortcut of Ctrl + shift + ñ. if you use another IDE, open a command console either powershell or cmd. 3

  3. in the window of the terminal go to the folder where you will create your project in my case in D:\frameworks. for this we will use the command cd D:/frameworks 4

  4. para crear nuestro proyecto utilizamos el siguiente comando en nuestra terminal rails new my_first_application 5

  5. When you have finished installing the application you should show something like this: 6

In this case, it indicates that the application was created but that we have problems with the gem of sqlite3 which is the default database for rails.

  1. At the moment we are going to deactivate the gem sqllite3 and to execute the command bundle install in the terminal to install all the gems.

In visual studio code first of all we must open the folder of the application that we just created.

choose the option of Open folder or with the shortcut Ctrl+k+o

We look for our project folder according to the route that I keep it, we select it and we give it in the option to select folder 7

will show us something like this 8 now we open the Gemfile file and comment on line 12 that corresponds to the gem of sqllite3 9

It should look like this 10

we give in saving or with the shortcut of Ctrl + s and execute the command bundle install in the terminal, do not forget that the commands that you are going to execute must be done in the route where the project was created 11 12

if you get this we will proceed to comment the line 40 of the Gemfile corresponding to the gem of capybara (do not forget to save changes) and execute the command of bundle install again.

at the end it should show up like this 13 When executing the bundle install command all the gems that are in the gemfile file will have been installed except sqllite3 and capybara that were commented

We must re-enter the Gemfile file and re-comment the line referring to sqllite3 like this: 14 We are ready to execute our application in white, in the terminal we enter the folder bin (cd bin) and execute the command rails server 15 If everything is correct, it should show you this way. 16

Enter the following url http://localhost:3000 in a browser should show something like the following. 17

This means that your application is working and ready to work.

  1. we will work with the sqllite3 database, then we must install the rails dev kit in order to download sqllite and compile it in our pc. we enter the following url: https://rubyinstaller.org/downloads/ 18

We select the one that appears in the red box of the image 19

Once downloaded, close visual studio and install the devkit with the default options. 20

21

23

In this window you press option 1 and then enter option 2

22

When the installation is finished, the window can be closed

Having installed the devkit will have changed an environment variable in the operating system that we must later adjust, see how it was:

Enter to this computer (My pc) then you give right click properties 23

will show you something like that 24

we go to advanced configuration of the system then to variables of environment we select pathy finally in edit 25 26

As you can see, the devkit folder (C:\Ruby25-x64\bin) is first in priority with respect to the folder of the rails that we had installed before (C:\RailsInstaller\Ruby2.3.3\bin) that means that the operating system will first search for the executables in the folder in the devkit and then in the rails folder.

Remember that the purpose of having downloaded the devkit is only to download the sqllite and compile it in our machine to use it in our project so later we will change that priority.

You can click on cancel in the windows to close them.

In case it shows you this way, you must select (C: \ Ruby25-x64 \ bin) and give it the option to upload. 27

it should be like this

28

Once this is done, you are given the option to accept.

  1. We are ready to download sqlite and compile it on our machine, open a command console (cmd) 29

go to the path where you save your project and execute the following command ridk exec pacman -S mingw-w64-x86_64-dlfcn

I will ask you if you want to continue, tell her that S 30

If everything went well you should have left 100% for all tasks.

You can now close the command console

  1. Enter the visual studio code and make sure you are located in the project folder 31

Let's run the following command in the terminal to add the gem of sqllite 3: gem install sqlite3 -v 1.4.0

It should show like this 32

So far we have installed the gem of sqllite3 in our project. We are going to prove that it continues to work correctly, for that it closes visual studio code. We are going to change the execution order of the environment variables:

Enter to this computer (My pc) then you give right click properties 23

will show you something like that 24

we go to advanced configuration of the system then to variables of environment we select pathy finally in edit 25 We are going to change the order of the execution of the environment variables, select the variable of the path of the lanes (C:\RailsInstaller\Ruby2.3.3\bin) and click on the upload button to locate it before the kit variable (C:\Ruby25-x64\bin) 33

It should look like this: 34

Click on accept in all windows and reopen visual studio code. 35

Being in the project route let's run the server again cd bin after rails server 36

If everything went well the server had to have started and is listening by port 3000 37

If you have this page in the browser it is because the installation and configuration of the sqllite gem was successful.

You can stop the server by pressing Ctrl + c

  1. let's use the mvc pattern we should start by creating the model Let's create a model called user with the fields name, age, phone, email, city. For that we are going to execute the following command (the model must be named in singular and in English so that rails can pluralize it when creating the table in the database).

rails generate model user name:string phone:string email:string

if everything went well it should show up like this 38

With this we have created a migration in the db/migrate folder of the project: 39

We are going to make the migration effective by executing the following command

rake db: migrate

The output should be: 40

We will confirm if the migration was successful.

we go to the app folder then to the models folder and we will find our user model 41

With this we have already created our first and additional table we have our model

Rails created us the user.rb class within the app / models folder that corresponds to our model and although it seems to be empty when using applicationrecord (inherits from that class) is associated with our users table (eye the table was created in plural, although we have created our model in the singular) that we previously created

  1. we are going to create a controller that allows us to obtain the data of the User model and show them in a view.

To add a driver we execute the following command in the terminal:

rails generate controller Usuarios index

in this case Usuarios: is the name of the controller index is the name of the action therefore a view called index should be created.

If everything went well the output should be.

When executing the command in the controllers folder we will find a new controller and a new view. 42

We are going to program our controller to add, modify, delete and bring all the records of the model to paint them in the view.

  1. let's program our methods in the controller 43

  2. We are going to program our view so that it can process all the rows sent by the controller using the @user variable

Open the index.html.erb view

It must be like this

44

We are going to program in such a way that it implements a cycle that iterates over the registers of the @user variable the view should look like this.

45

  1. Restart the server open the terminal with the command cd bin and then with the command rails server

46

Enter in a browser to the url: http://localhost:3000/users/index

You should be able to look at a browser as well as the following image

47

As we do not have any data in the database, the table appears empty.

You can stop the server by pressing Ctrl + c

to add a new record we must create a new view called new since it must be equal to the controller method.

we go to the folder views in the folder of users we give right click and select new file

48

now you must name it as new.html.erb

49

finally we program the view to add a new record and also we will reuse it to edit. the view should look like this.

50

finally we must update the routes of our project.

we go to the config folder and open the routes file.

the routes should stay like this:

51

  1. Restart the server open the terminal with the command cd bin and then with the command rails server

Enter in a browser to the url: http://localhost:3000/users/index

we give in the add link to add a new record.

52

Our registration form will appear.

Fill in the fields and click on the add button to add it.

53

redirects automatically to the index and will show us the data that we just registered.

54

Add another record with false data to edit them.

55

now we give in the edit link to edit the fields.

56

It will show us our form but with the fields that we previously registered.

57

edit the fields and we give in the update button.

58

If everything is fine, it redirects us to the index showing the updated data

59

and to finish our project we give in the link of delete and automatically we will delete the record.

60

If everything works correctly you should automatically delete it and show the existing records

61