Translate

Friday, June 5, 2015

AngularJs + Symfony2 Dealing with routes


This tutorial intends to explain how to integrate Symfony2 routes and angularJs routes in order to understand how they are going to  communicate.


Step1: First of all, we need to install a bundle called FOSJsRoutingBundle that will help us to access to the symfony2 route from javascript.

Actually,it's easy to install this is the GIT repository: FOSJsRoutingBundle
And this is installation Method : How to install FOSJsRoutingBundle


Step2: Configure our symfony2 routes.We need to create our routes to access  specific actions or method.

in the file: src/REVC/BlogBundle/Resources/config/routing.yml
  

rou_dispacher:
    path:     /dispath/{template}
    defaults: { _controller: REVCBlogBundle:Default:dispatch }
    options:
        expose: true



Because we are using the bundle FOSJsRoutingBundle, we need to expose our route.To avoid creating routes for each action,I've designed this route to receive a parameter which is the template name.


Step3: Create our controller for the route created.

in the file: src/REVC/BlogBundle/controller/ArticleController.php
  

       public function dispatchAction($template)
        {
            return $this->render('REVCBundle:'.$template);
        }

  

Having that controller, you will be able to access to any  template you want.


Of course, don't forget to  create yours template in :src/REVC/BlogBundle/Resources/views/Article

Step4: Finally, we need to create our AngularJs route:

I have created my angular files in : src/REVC/BlogBundle/Resources/public/js/app.js

 app.config(['$routeProvider',function($routeProvider){

        $routeProvider
                .when('/home',{ 
                           templateUrl: Routing.generate('rou_dispacher',{template:"Article:home.html.twig"})
               })
                .when('/',{
                           templateUrl: Routing.generate('rou_dispacher',{template:"Article:home.html.twig"})
               })
                .when('/edit',{
                           templateUrl: Routing.generate('rou_dispacher',{template:"Article:edit.html.twig"}) 
                })
                .when('/create',{
                           templateUrl: Routing.generate('rou_dispacher',{template:"Article:create.html.twig"}) 
                });
 
    }]);



As you can see , I have used the functions called Routing.generate that allows us to communicate with the symfony2 route.

Basically, the Routing.generate will accept the following parameters:

Routing.generate('route_name',{variable});

Having that configuration, it will be easy to work with AngularJs and Symfony2.

This is the screen shot  about the structure folder of my application.






 

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

Phenomenon Disaster Videos

Home Destroyed by Furious Australia Storm



Tornado Disaster