Translate

Showing posts with label Ruby on Rails. Show all posts
Showing posts with label Ruby on Rails. Show all posts

Tuesday, July 25, 2023

The Feasibility of Learning Ruby as Your First Programming Language

 

Ruby as Your First Programming Language



Choosing the right programming language for beginners is crucial as it sets the foundation for a successful journey in the world of coding. Ruby, a powerful and user-friendly language, has gained popularity for its simplicity and versatility. In this article, we'll explore the feasibility of learning Ruby as the first programming language and discuss the benefits it offers to aspiring developers.


Beginner-Friendly Syntax

Ruby boasts an elegant and human-readable syntax, making it an excellent choice for beginners. Its code is concise and easy to understand, allowing novices to quickly grasp fundamental programming concepts. The language is designed to prioritize developer happiness, reducing the barriers to entry for newcomers and promoting an enjoyable learning experience.


Abundance of Learning Resources

As one of the most widely used programming languages, Ruby has a wealth of learning resources available online. From comprehensive tutorials to interactive coding platforms, beginners can find ample support to kick-start their programming journey. Moreover, the Ruby community is known for its friendliness and willingness to help, making it an inclusive environment for learners.


Versatility and Popularity

Ruby's versatility is another reason why it is a feasible first programming language. It is used extensively for web development, with the popular Ruby on Rails framework providing a robust platform to build web applications. Additionally, Ruby is employed in automation scripts, data analysis, and game development, showcasing its wide-ranging capabilities.


Encourages Clean Code Practices

Ruby's philosophy emphasizes clean and readable code. As beginners learn Ruby, they naturally adopt best coding practices, fostering good habits from the start. This focus on readability not only helps newcomers understand code more easily but also promotes teamwork and maintainability in larger projects.


Rapid Prototyping and Development

For beginners aiming to see tangible results quickly, Ruby excels at rapid prototyping and development. Its dynamic nature allows for swift iteration, enabling developers to experiment and see their ideas come to life without being bogged down by overly complex syntax or rigid structures.


Community and Job Opportunities

Choosing a popular programming language like Ruby means becoming part of a large and thriving community. This opens doors to numerous job opportunities and networking possibilities, both locally and globally. Many startups and established companies value Ruby skills, making it a viable language for career growth.


Transferable Skills

Learning Ruby doesn't just confine beginners to one language; it equips them with transferable skills applicable to other languages and paradigms. The core programming concepts acquired through Ruby can be easily translated to other languages like Python, JavaScript, or Java, providing a solid foundation for future learning.


In conclusion, learning Ruby as the first programming language is indeed feasible and can be a wise choice for beginners. Its beginner-friendly syntax, abundant learning resources, versatility, and emphasis on clean code practices make it an excellent language to start your coding journey. Additionally, the supportive Ruby community and job opportunities further solidify its appeal. Aspiring developers who choose Ruby will not only gain proficiency in this dynamic language but also build the necessary skills to succeed in the ever-evolving world of programming.



Friday, March 10, 2023

Selenium doesn't work with Bootstrap 5: Cannot click on button

 



Selenium test code with Bootstrap 5




Bootstrap 5, a powerful CSS framework, brings new challenges. One issue developers face is Selenium test failures due to Bootstrap 5's smooth scrolling feature. In this article, we'll explore this problem and provide solutions.

 Recently, I encountered some unexpected test suite failures while using Bootstrap 5. Upon closer inspection, I noticed that the test cases were randomly failing, particularly when interacting with the save button as it will not submit the form. An intriguing puzzle, isn't it?

To address the perplexing test failures, I embarked on an extensive debugging process and conducted thorough research. Eventually, I uncovered the root cause of the issue: a conflict between Bootstrap 5 and Selenium. Specifically, a particular CSS element was causing the problem.




 scroll-behavior: smooth;




This does not allow Selenium to automatically scroll down and click on the element that is at the bottom of the page, I mean an element that is outside window.



How to fix it?


I found 2 ways to fix this kind of flaky tests. I tested them and they actually works. 😊


1. Disable smooth scrolling from bootstrap.

 Note: if you are using rails it should be in: _bootstrap-custom.scss


 $enable-smooth-scroll: false;

2.  Override bootstrap scroll-behavior.



 html {
    scroll-behavior: auto !important;
}


2.1 if you're using rails, you can actually do this just for the test environment:



 html[data-environment="test"] {
    scroll-behavior: auto !important;
}


and in your haml file you should have something like this:


%html{"data-environment": "#{Rails.env}"}














Wednesday, March 23, 2022

Configuring Capistrano to run migrations on multiple servers

 



A few months ago I found myself in a situation where I had to run the migrations on a second server. After some research, I learned that Capistrano only runs migrations on the first server in the :db group, so other servers with :db will be ignored.


I was very surprised!!!

I kept looking into this and finally found the option I was looking for:


This is an example of the config/deploy/production.rb

 server 'sample-web-server1', user: 'deploy', roles: %w{app web db}
 server 'sample-web-server2', user: 'deploy', roles: %w{app web}
server 'sample-web-server3', user: 'deploy', roles: %w{app web db}

Now in your config/deploy.rb file add this option:

 set :migration_servers, -> { release_roles(fetch(:migration_role)) }

Now, you just have to deploy.


As a side note:

According to the capistrano changelogs, this option was introduced from version 1.1.7, so if you have capistrano 1.1.7 onwards, you can safely use it. 

You can also check the capistrano documentation here


Tuesday, November 1, 2016

Ruby on Rails: How to upgrade bundler







To upgrade bundler, just type this command in to  the command line:
gem install bundler

To check if it was upgraded , just display the new version by typing the following command:

bundle --version

Friday, September 2, 2016

Rails - How to enable asset pipeline in development


Sometimes, for debugging purpose, it can be quite helpful to enable the Rails assets pipeline in development model.


to do this, go to config/enviroments/development.rb and add the following configuration:

config.assets.debug = false
config.assets.compile = false
config.assets.digest = true

An important thing to keep in mind is be sure  to have  config.assets.debug = true (the default setting) is disabled.

Finally, you only need to restart your server and then run:

rake assets:precompile

Friday, June 17, 2016

Ruby on Rails Remove Blank elements from array





Sometimes, when you are working with arrays it is possible to have  empty elements,therefore you will need to clean it. In this section I will show you how to remove blank elements from a array using either Ruby on Rails or just Ruby.

Given the array called test
=> ["test", "", 1, 2, ""]

so, to remove the blank elements this is the syntax:

for Ruby on Rails:

test.reject &:blank? 

For Ruby:
  test.reject &:empty? 

Wednesday, August 5, 2015

Routing Error No route matches [GET] "/users/logout"




The message "Routing Error No route matches [GET] "/users/logout" is because you are sending the request via get and should be delete, so to fix it you have two options:

 The first one  is the  better, change the tag  link_to to button_to, doing that, rails will  do  the dirty work to send it as  a delete request.

The second one is not recommended, since DELETE is the appropiete RESTFUL query , but it works. Got to devise.rb change  config.sign_out_via = :delete to config.sign_out_via = :get



 

Sunday, May 31, 2015

Adding HTML tag in Rails link_to


Rails



Recently ,I've been working on Ruby on Rails and  had to do something  like the example shown below, but using the helper link_to:

Example:

    
 
<a data-method="delete"rel="nofollow" href='sign_out'>
<i class="fa fa-sign-out fa-fw"</i>Logout
</a>


After researching, I found the answer, you should add  your html to the link_to helper as shown below:


Wednesday, May 20, 2015

The most useful Rails 4 commands




These are some useful rails commands:


  • rails -v: Gets the rails version.
  • rails new: Generates the  application skeleton  and base files. (e.g rails new myBlog)
  • rails dbconsole: shows the database that  is currently on your system .
  • rails s or rails server: launches the web server (webrick)
  • rake db:migrate: Runs the migrations files that have not been run.
  • rails generate model: Creates the model and will create a migration file
  • rails generate controller: creates the controller in your application
  • rails generate model your_model_name --skip-migration: Generates the model but will not create the migration file.
  • rails generate migration : creates a migration file.
  • rails d migration your_migration_name:removes the migration file specify
  • rake db:rollback: roll back the most recent migration
  • rake routes:  Displays all routes configured in your rails application