Translate

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 27, 2015

ERROR 1701 (42000): Cannot truncate a table referenced in a foreign key constraint


If you have ever had this error, is because you want to truncate a table that has foreign key in another table. Let me put it this way.

Having this  structure, where "articles" can have multiple users , and you want to delete the table users using the function TRUNCATE, Mysql will throw this error: ERROR 1701 (42000): Cannot truncate a table referenced in a foreign key constraint


There is a way to avoid this error, but you have to understand,  if you want to delete the  users tables and reset the auto increment field , you have to do the same with the articles table.

This is a basic example, but what would happen if you want to clean an entire database and reset the auto increment? That would be very complicated without the following code.


I know this code could be useful for you:



SYNTAX:
   

     SET foreign_key_checks = 0;

       TRUNCATE  `users`;


       TRUNCATE  `articles`;


     SET foreign_key_checks = 1;

 
Doing that, you will temporarily disabling  referential constraint, and at the end you will enable the referential constraint.

How to Reset auto increment after deleting a table row





To delete data and reset the auto increment field using MYSQL, we have a function called TRUNCATE which will help you to accomplish this task.


Syntax:

   

     TRUNCATE  `articles`;

 
Basically , After the function "TRUNCATE" you have to write your table name , in this case is the table called " ARTICLES" .When you run this SQL sentence you will delete the whole data and the auto increment field will be reset.

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

Tuesday, May 19, 2015

MYSQL Save the Last Id Inserted in a Variable







To save the last id inserted in a SQL variable , we need to do this:


INSERT INTO Users (user_name, user_email, user_address, user_city)
VALUES ('bryan.hamilton','Tbha@gmail.com','Av.21','ohio'); 

//we create a new variable called userLastId
 SET @userLastId = LAST_INSERT_ID( );

//now, we can use this varable in another sql sentence

INSERT INTO articles (art_title, art_body, art_user_id, art_created_at)
VALUES ('Geologic time scale','my body ..',@userLastId ,NOW());  

 
Basically, when you save the last id inserted in the variable that value is available to use it in others SQL sentences.

Monday, May 18, 2015

Friday, May 15, 2015

Convert a date into Timestamp in Javscript



To convert a date into timestamp milisecond  in JavaScript we are going to use the function new Date() that JavaScript provide  to deal with dates.

Basically, this is code:


//convert date into a timestamp milisecond
var oDate = new Date('2015','05','12','07','30','00');

//printing the timestamp   
console.log(oDate.getTime());


Explanation:

The function  Date() , receives the following parameters:
new Date(year, month, day, hours, minutes, seconds, milliseconds)

so,the Object oDate(Variable) has a method called getTime, that you can access to convert the date into a timeStamp.


Output:

1434115800000


NOTE: In javascript, the conversion to timestamp is in timestamp millisecond and not timestamp epoch. 


Wednesday, May 13, 2015

Regular Expressions You Should Know(REGEX)


In this section, I will be writing some REGEX that could be helpful  to your  projects.



REGEX #1: Numbers Separated by comma or blank spaces.Example(1,2 ,3  ,4)



   //Numbers Separated by comma or blank spaces
   ^[\d]{1,}(\s*,\s*[\d]{1,})+$
 
 

REGEX #2:  Only number


   //Only Number
   ^(\d)+$
 
 
REGEX #3: Alphanumeric


   //Only Alphanumeric 
   ^(\w)+$
 
 
REGEX #4: Validate URL (either http, https or IP number)

   //Validate URL (either htpp,https or IP number)
   ^(http|https):\/\/([\w+?\.\w+])+([a-zA-Z0-9\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?$
 
 
REGEX #5: Validate Emails. Example : myUser.test01@gmail.com

   //Validate emails
   ^[\w_.+-]+@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$
 
 

REGEX #6: Validate Hours format

//Validate Hours Format
^[\d]{1,2}:[\d]{1,2}:[\d]{1,2}$



REGEX #7: Validate date format

//Validate date Format
^[\d]{2}\/[\d]{2}\/[\d]{4}$



REGEX #8: Validate IP number format (IPV4)

//Validate IP number format (IPV4)
^[\d]{1,3}.[\d]{1,3}.[\d]{1,3}.[\d]{1,3}$



REGEX #9: Regular expression to limit number of characters to 10

//Limit number of characters to 10
^[a-zA-Z]{1,10}$



REGEX #10: Regular expression: Match Not equal to the string "login"

//Match Not Equal to the string "login"
^(?!login$).+$


Monday, May 11, 2015

How to Delete an element from an array?





In this post, we will answer the question : How to Delete an element from an array?

First of all , we  will create our own array called :$aFruits


  
   $aFruits = array('apple','lemon','grapes','pineapple');
 

Secondly,  if we want to delete the lemon element, we have to use the function called unset and we need to access to  that element by  its  position number.


  
  unset($aFruits[1]);
 

Ouput:
 Array ( [0] => apple [2] => grapes [3] => pineapple )