Advertisement
Guest User

Untitled

a guest
Nov 28th, 2017
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 23.58 KB | None | 0 0
  1. 1a
  2.  
  3. <html>
  4.     <head>
  5.         <title>Fibonacci</title>
  6.     </head>
  7.     <body>
  8.         <script type = text/javascript>
  9.         function fibo(n)
  10.         {
  11.             var res;
  12.             if(n==0)
  13.             {
  14.                 res = 0;
  15.             }
  16.             else if(n==1)
  17.             {
  18.                 res = 1;
  19.             }
  20.             else
  21.             {
  22.                 res = fibo(n-1)+fibo(n-2);
  23.             }
  24.             return res;
  25.         }
  26.         function PrintFibo(num)
  27.         {
  28.             var i;
  29.             document.write("<h2><b/><i/>The Fibonacci Series for "+a+"</h2>\n");
  30.             for(i=0;i<=num;i++)
  31.             {
  32.                 document.write(fibo(i) + "<br />");
  33.             }
  34.         }
  35.         var a = prompt("Enter a number");
  36.         PrintFibo(a);
  37.        
  38.         </script>
  39.     </body>
  40. </html>
  41.  
  42. 1b
  43.  
  44.  
  45. <html>
  46.     <head>
  47.         <title>Squares</title>
  48.     </head>
  49.     <body>
  50.         <script type = "text/javascript">
  51.             var msg = "The Number and it's squares are \n";
  52.             var n = prompt("Enter a number");
  53.             var i;
  54.             for(i=1;i<=n;i++)
  55.             {
  56.                 msg = msg+i+" - "+i*i+"\n";
  57.             }
  58.             alert(msg);
  59.         </script>
  60.     </body>
  61. </html>
  62.  
  63. 2a
  64.  
  65.  
  66. <html>
  67. <head>
  68.     <title>USN Validation</title>
  69. </head>
  70. <body bgcolor="lightblue">
  71.     <script type="text/javascript">
  72.         function func(USN)
  73.         {
  74.             var pattern1=/^[1-4][A-Z]{2}[0-9]{2}[A-Z]{2}[0-9]{3}$/
  75.             if(!USN.value.match(pattern1)||USN.value.length==0)
  76.             {
  77.                 alert("Invalid USN")
  78.                 return false
  79.             }
  80.             else
  81.                 alert("USN ("+USN.value+") is Valid")
  82.         }
  83.     </script>
  84.     <form action="">
  85.             <h3> Enter Your USN Number: </h3>
  86.             <input type="text" name="USN">
  87.             <br />
  88.             <br />
  89.             <input type="submit" value="submit" onclick="func(USN)"/>
  90.             <input type = "reset" value="reset"/>
  91.     </form>
  92. </body>
  93. </html>
  94.  
  95.  
  96.  
  97. 2b
  98.  
  99.  
  100. <html>
  101. <head>
  102. <title>USN and Semester Validation</title>
  103. </head>
  104. <body bg color = "lightblue">
  105. <script type="text/javascript">
  106. function disp(usn,sem)
  107. {
  108.     var pattern1=/^[1-4][A-Z]{2}[0-9]{2}[A-Z]{2}[0-9]{3}$/
  109.     if(!usn.value.match(pattern1)||usn.value.length==0)
  110.     {
  111.         alert("Invalid USN!\nEnter a valid USN")
  112.         return false;
  113.     }
  114.     else
  115.         alert("USN valid!");
  116.     var pattern2=/^[1-8]$/
  117.     if(!sem.value.match(pattern2)||sem.value.length==0)
  118.     {
  119.         alert("Invalid Semester!\nEnter a valid Semester");
  120.         return false;
  121.     }
  122.     else
  123.         alert("Semester valid!");
  124. }
  125. </script>
  126. <form action="">
  127. <pre>
  128. Enter the Usn
  129. <input type="text" name="usn" /> <br/>
  130.  
  131. Enter the semester
  132. <input type="text" name="sem" /> <br/>
  133. <input type="submit" value="submit" onclick="disp(usn,sem)" />  <input type="reset" value="reset" />
  134. </pre>
  135.     </form>
  136. </body>
  137. </html>
  138.  
  139.  
  140.  
  141.  
  142. 3a
  143.  
  144.  
  145. <html>
  146. <head>
  147.     <title>Program to display Stacked Paragraphs</title>
  148.     <style type="text/css">
  149.     .layer1Style
  150.     {
  151.         border:solid thick black;
  152.         padding:1em;
  153.         width:500px;
  154.         background-color:pink;
  155.         position:absolute;
  156.         top:100px;  left:200px;
  157.     }
  158.  
  159.     .layer2Style
  160.     {
  161.         border:solid thick black;
  162.         padding:1em;
  163.         width:500px;
  164.         background-color:yellow;
  165.         position:absolute;
  166.         top:120px;  left:220px;
  167.     }
  168.  
  169.     .layer3Style
  170.     {
  171.         border:solid thick black;
  172.         padding:1em;
  173.         width:500px;
  174.         background-color:cyan;
  175.         position:absolute;
  176.         top:140px;
  177.         left:240px;
  178.     }
  179. </style>
  180.     <!--<link rel="stylesheet" type="text/css" href="Program3a.css"/>-->
  181.     <script type="text/javascript">
  182.     var topLayer="layer3";
  183.     function stack(toTop)
  184.     {
  185.         var oldTop=document.getElementById(topLayer).style;
  186.         var newTop=document.getElementById(toTop).style;
  187.         oldTop.zIndex="0";
  188.         newTop.zIndex="10";
  189.         topLayer=document.getElementById(toTop).id;
  190.     }
  191.     </script>
  192.    
  193.     <body style="background-color:orange">
  194. <h3 style="text-align:center"> Program to demonstrate the usage of stacking of Paragraphs</h3>
  195.     <div class="layer1Style" id="layer1" onmouseover="stack('layer1')">
  196. Welcome to JIT. This college offers courses like computer science and engineering. Info science,Electrical and Electronics.
  197.     </div>
  198.     <div class="layer2Style" id="layer2" onmouseover="stack('layer2')">
  199. The College also offers courses like MTech - Computer science and Information Science and Master of Business Administration.
  200.     </div>
  201.     <div class="layer3Style" id="layer3" onmouseover="stack('layer3')">
  202. The college has an excellent infrastructure, beautiful ambience, separate library and admin blocks, A huge playground and a hostel facilities.
  203.     </div>
  204. </body>
  205. </html>
  206.  
  207.  
  208.  
  209. 3b
  210.  
  211.  
  212.  
  213. <html>
  214. <head>
  215.     <title>Program to display Stacked Paragraphs</title>
  216.     <script type="text/javascript">
  217.     var stack1="stack1";
  218.     function toTop(curStack)
  219.     {
  220.         var oldStack=document.getElementById(stack1).style;
  221.         oldStack.zIndex="0";
  222.         var newStack=document.getElementById(curStack).style;
  223.         newStack.zIndex="10";
  224.         stack1=curStack;
  225.     }
  226.     </script>
  227.     <style type="text/css">
  228.     .para1
  229.     {
  230.         border:solid thick black;
  231.         padding:1em;
  232.         width:500px;
  233.         background-color:pink;
  234.         position:absolute;
  235.         top:100px;  left:200px;
  236.     }
  237.  
  238.     .para2
  239.     {
  240.         border:solid thick black;
  241.         padding:1em;
  242.         width:500px;
  243.         background-color:yellow;
  244.         position:absolute;
  245.         top:120px;  left:220px;
  246.     }
  247.  
  248.     .para3
  249.     {
  250.         border:solid thick black;
  251.         padding:1em;
  252.         width:500px;
  253.         background-color:cyan;
  254.         position:absolute;
  255.         top:140px;
  256.         left:240px;
  257.     }
  258. </style>
  259.    
  260.     <body style="background-color:orange">
  261. <h3 style="text-align:center"> Program to demonstrate the usage of stacking of Paragraphs</h3>
  262.     <p class="para1" id="stack1" onmouseover="toTop('stack1')" onmouseout="toTop('stack3')">
  263. Welcome to JIT. This college offers courses like computer science and engineering. Info science,Electrical and Electronics.
  264.     </p>
  265.     <p class="para2" id="stack2" onmouseover="toTop('stack2')" onmouseout="toTop('stack3')">
  266. The College also offers courses like MTech - Computer science and Information Science and Master of Business Administration.
  267.     </p>
  268.     <p class="para3" id="stack3" onmouseover="toTop('stack3')" onmouseout="toTop('stack3')">
  269. The college has an excellent infrastructure, beautiful ambience, separate library and admin blocks, A huge playground and a hostel facilities.
  270.     </p>
  271. </body>
  272. </html>
  273.  
  274.  
  275.  
  276. 4a.xml
  277.  
  278.  
  279. <?xml version = "1.0"?>
  280. <?xml-stylesheet type="text/css" href="4a.css"?>
  281. <VTU>
  282.     <student>
  283.         STUDENT INFO
  284.         <USN>1JT15CS001</USN>
  285.         <name>Arun</name>
  286.         <college>JIT</college>
  287.         <branch>CSE</branch>
  288.         <YOJ>2015</YOJ>
  289.         <email>arun@gmail.com</email>
  290.     </student>
  291.     <student>
  292.         <USN>1JT15CS002</USN>
  293.         <name>Gopal</name>
  294.         <college>JIT</college>
  295.         <branch>CSE</branch>
  296.         <YOJ>2015</YOJ>
  297.         <email>gopal@gmail.com</email>
  298.     </student>
  299.     <student>
  300.         <USN>1JT15CS003</USN>
  301.         <name>Sanjay</name>
  302.         <college>JIT</college>
  303.         <branch>CSE</branch>
  304.         <YOJ>2015</YOJ>
  305.         <email>san@gmail.com</email>
  306.     </student>
  307. </VTU>
  308.  
  309.  
  310.  
  311. 4a.css
  312.  
  313. USN
  314. {
  315.     display:block;
  316.     color:blue;
  317.     font-style:italic;
  318. }
  319. college
  320. {
  321.     display:block;
  322.     color:green;
  323.     font-style:italic;
  324. }
  325. branch
  326. {
  327.     display:block;
  328.     color:red;
  329.     font-style:italic;
  330. }
  331. email
  332. {
  333.     display:block;
  334.     color:blue;
  335.     font-style:italic;
  336. }
  337.  
  338.  
  339.  
  340. 4b.xml
  341.  
  342.  
  343. <?xml version = "1.0" encoding="ISO-8889-1"?>
  344. <?xml-stylesheet type="text/xsl" href="4b.xsl"?>
  345. <VTU>
  346.     <student>
  347.         <name>Arun</name>
  348.         <usn>1JT15CS001</usn>
  349.         <college>JIT</college>
  350.         <branch>CSE</branch>
  351.         <year>2015</year>
  352.         <email>arun@gmail.com</email>
  353.     </student>
  354.     <student>
  355.         <name>Gopal</name>
  356.         <usn>1JT15CS002</usn>
  357.         <college>JIT</college>
  358.         <branch>CSE</branch>
  359.         <year>2015</year>
  360.         <email>gopal@gmail.com</email>
  361.     </student>
  362.     <student>
  363.         <name>Sanjay</name>
  364.         <usn>1JT15CS003</usn>
  365.         <college>JIT</college>
  366.         <branch>CSE</branch>
  367.         <year>2015</year>
  368.         <email>san@gmail.com</email>
  369.     </student>
  370. </VTU>
  371.  
  372.  
  373.  
  374. 4b.xsl
  375.  
  376.  
  377. <?xml version = "1.0" encoding="ISO-8889-1"?>
  378. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  379. <xsl:template match="/">
  380. <html>
  381.     <body>
  382.         <h2>VTU Student Information</h2>
  383.         <table border="1">
  384.             <tr bgcolor="#99cd32">
  385.                 <th>name</th>
  386.                 <th>usn</th>
  387.                 <th>collegeName</th>
  388.                 <th>branch</th>
  389.                 <th>year</th>
  390.                 <th>email</th>
  391.             </tr>
  392.         <xsl:for-each select="VTU/student">
  393.         <xsl:choose>
  394.             <xsl:when test="name='Gopal'">
  395.             <tr bgcolor="#ff00ff">
  396.                 <td><xsl:value-of select="name"/></td>
  397.                 <td><xsl:value-of select="usn"/></td>
  398.                 <td><xsl:value-of select="college"/></td>
  399.                 <td><xsl:value-of select="branch"/></td>
  400.                 <td><xsl:value-of select="year"/></td>
  401.                 <td><xsl:value-of select="email"/></td>
  402.             </tr>
  403.             </xsl:when>
  404.             <xsl:otherwise>
  405.             <tr>
  406.                 <td><xsl:value-of select="name"/></td>
  407.                 <td><xsl:value-of select="usn"/></td>
  408.                 <td><xsl:value-of select="college"/></td>
  409.                 <td><xsl:value-of select="branch"/></td>
  410.                 <td><xsl:value-of select="year"/></td>
  411.                 <td><xsl:value-of select="email"/></td>
  412.             </tr>
  413.             </xsl:otherwise>
  414.         </xsl:choose>
  415.         </xsl:for-each>
  416.         </table>
  417.         <h2>Selected student is highlighted</h2>
  418.     </body>
  419. </html>
  420. </xsl:template>
  421. </xsl:stylesheet>
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428. 5a.pl
  429.  
  430.  
  431. #! /usr/bin/perl
  432.  
  433. use CGI ':standard';
  434.  
  435. print header;
  436. print start_html("Lab 5a");
  437. print "SERVER INFORMATION <br/><br/>";
  438. print "Server Name = $ENV{'SERVER_NAME'} <br/><br/>";
  439. print "Server Software = $ENV{'SERVER_SOFTWARE'}<br/><br/>";
  440. print "Server Protocol = $ENV{'SERVER_PROTOCOL'}<br/><br/>";
  441. print "CGI Revision = $ENV{'GATEWAY_INTERFACE'}<br/><br/>";
  442. print end_html;
  443.  
  444.  
  445.  
  446. 5b.html
  447.  
  448. <html>
  449.     <head>
  450.         <title>Lab 5b</title>
  451.     </head>
  452.     <body>
  453.         <form action ="http://localhost/cgi-bin/5b.pl" method="POST">
  454.         <h3>Enter the Unix command!</h3>
  455.         <input type = "text" name = "cmd"/>
  456.         <input type ="submit" value = "Execute"/>
  457.         </form>
  458.     </body>
  459. </html>
  460.  
  461.  
  462.  
  463. 5b.pl
  464.  
  465. #! /usr/bin/perl
  466.  
  467. use CGI':standard';
  468.  
  469. print header;
  470. print start_html("Lab 5b");
  471. $c=param('cmd');
  472. system($c);
  473. print end_html;
  474.  
  475.  
  476. 6a.html
  477.  
  478.  
  479. <html>
  480.     <head>
  481.         <title>Lab 6a</title>
  482.     </head>
  483.     <body>
  484.         <form action = "http://localhost/cgi-bin/6.pl">
  485.         <pre>
  486.         Enter Name:
  487.         <input type="text" name="usrname"/>
  488.         <input type = "submit" value = "Submit!"/>
  489.         </pre>
  490. </form>
  491. </body>
  492. </html>
  493.  
  494.  
  495. 6a.pl
  496.  
  497.  
  498. #! /usr/bin/perl
  499. use CGI':standard';
  500. print header;
  501. print start_html("Lab 6a");
  502. $input = param('name');
  503. print h2"HI $input<br/><br/>";
  504. my @msgs=("Welcome to Web Programming","Welcome to Computer science","Thank you for visiting web page","Hello");
  505. print "The message received is :";
  506. print $msgs[int rand scalar @msgs];
  507. print end_html;
  508.  
  509.  
  510.  
  511.  
  512. 6b.pl
  513.  
  514. #! /usr/bin/perl
  515. use CGI':standard';
  516. print header;
  517. print start_html("Lab 6b");
  518. open(FILE,'<count.txt');
  519. $a = <FILE>;
  520. $a++;
  521. close(FILE);
  522. open(FILE,'>count.txt');
  523. print FILE "$a";
  524. close(FILE);
  525. print h1("Number of visiters are $a");
  526. print end_html;
  527.  
  528.  
  529. 7.pl
  530.  
  531.  
  532.  
  533. #! /usr/bin/perl
  534. use CGI':standard';
  535. ($s,$m,$h) = localtime(time);
  536. $delay = 1;
  537. print "Refresh: ", $delay, "\n";
  538. print header;
  539. print start_html("Lab 7");
  540. print "Time = $h:$m:$s";
  541. print end_html;
  542.  
  543.  
  544. 8.html
  545.  
  546.  
  547. <html xmlns = "http://www.w3.org/1999/xhtml">
  548.     <form action="http://localhost/cgi-bin/8.pl" method ="post">
  549.         <pre>
  550.             Name:<input type="text" name="name"><br>
  551.             Age: <input type="text" name="age"><br>
  552.                 <input type="submit" value="Submit" />
  553.         </pre>
  554.         </form>
  555.      </html>
  556.  
  557.  
  558.  
  559.  
  560. 8.pl
  561.  
  562. #! /usr/bin/perl
  563. use CGI':standard';
  564. use DBI;
  565.  
  566. print header;
  567. print start_html("8");
  568. $n = param('usrname');
  569. $a = param('usrage');
  570.  
  571. $db = DBI -> connect("DBI:mysql:user","root","deepak");
  572. $st = $db -> prepare("insert into usrinfo values('$n','$a')");
  573. $st -> execute();
  574.  
  575. $st = $db -> prepare("select * from usrinfo");
  576. $st -> execute();
  577.  
  578. print "<table border = 1>
  579. <tr>
  580. <th> Name </th>
  581. <th> Age </th>
  582. </tr>";
  583.  
  584. while(($name,$age) = $st->fetchrow())
  585. {
  586.     print "<tr>
  587.     <td> $name </td>
  588.     <td> $age </td>
  589.     </tr>";
  590. }
  591.  
  592. print "</table>";
  593.  
  594. $st -> finish();
  595. $db -> disconnect();
  596.  
  597. print end_html;
  598.  
  599.  
  600.  
  601. 9.php
  602.  
  603.  
  604. <?php
  605. $inTwoMonths = 60*60*24*60+time();
  606. setcookie('lastVisit',date("h:i:s A - m/d/y"), $inTwoMonths);
  607. if(isset($_COOKIE['lastVisit']))
  608.     $visit=$_COOKIE['lastVisit'];
  609. else
  610.     echo "You've got some expired cookies!";
  611. echo "Your last visit was - ". $visit;
  612. ?>
  613.  
  614.  
  615.  
  616. 10.php
  617.  
  618. <?php
  619. session_start();
  620. if(isset($_SESSION['views']))
  621.     $_SESSION['views']=$_SESSION['views']+1;
  622. else
  623.     $_SESSION['views']=1;
  624. echo "Page views = ".$_SESSION['views'];
  625. ?>
  626.  
  627.  
  628. 11.html
  629.  
  630.  
  631. <html>
  632. <head>
  633.     <title>HTML, PHP and MySQL</title>
  634. </head>
  635. <body>
  636.     <h3>    Enter information</h3>
  637.     <form action="http://localhost/akshay11a.php" method="POST">
  638.         <pre>
  639. Name
  640. <input type="text" name="name" />
  641.  
  642. Address 1
  643. <input type="text" name="address1" />
  644.  
  645. Address 2
  646. <input type="text" name="address2" />
  647.  
  648. E-mail
  649. <input type="text" name="email" />
  650.  
  651. <input type="submit" value="Submit" />  <input type="reset" value="Clear" />
  652.         </pre>
  653.     </form>
  654.    
  655.     <form action="http://localhost/akshay11b.php" method="POST">
  656.         <pre>
  657. Enter the name
  658. <input type="text" name="name" />
  659.  
  660. <input type="submit" value="Submit" />
  661.         </pre>
  662.     </form>
  663. </body>
  664. </html>
  665.  
  666.  
  667.  
  668.  
  669. 11a.php
  670.  
  671.  
  672.  
  673. <?php
  674.  
  675. $name=htmlspecialchars($_POST['name']);
  676. $address1=htmlspecialchars($_POST['address1']);
  677. $address2=htmlspecialchars($_POST['address2']);
  678. $email=htmlspecialchars($_POST['email']);
  679.  
  680. $connection = mysql_connect("localhost", "root", "jit123") or die(mysql_error());
  681. mysql_select_db("akshay") or die(mysql_error());
  682. $sql = "insert into user(name, addr1, addr2, email) values ( '$name', '$address1', '$address2', '$email');";
  683.  
  684. mysql_query($sql) or die(mysql_error);
  685. echo "Values inserted";
  686.  
  687. ?>
  688.  
  689.  
  690. 11b.php
  691.  
  692.  
  693. <html>
  694. <body>
  695.  
  696.  
  697. <?php
  698.  
  699. $name=htmlspecialchars($_POST['name']);
  700.  
  701. $connection = mysql_connect("localhost", "root", "jit123") or die(mysql_error());
  702. mysql_select_db("akshay") or die(mysql_error());
  703. $sql = "select * from user where name like '$name%';";
  704.  
  705. $result = mysql_query($sql) or die(mysql_error);
  706. ?>
  707.  
  708. <table border="1">
  709. <tr><th>Name</th><th>Address 1</th><th>Address 2</th><th>E-Mail</th></tr>
  710.  
  711. <?php
  712.  
  713. while($cols=mysql_fetch_row($result))
  714. {
  715.    echo "<tr>
  716.             <td>$cols[0]</td>
  717.             <td>$cols[1]</td>
  718.             <td>$cols[2]</td>
  719.             <td>$cols[3]</td>
  720.             </tr>";
  721. }
  722.  
  723. ?>
  724. </table>
  725. </body>
  726. </html>
  727.  
  728.  
  729.  
  730.  
  731. 12
  732.  
  733.  
  734.  
  735.  
  736. ## Program 12:
  737. ### Build a Rails application to accept book information viz. Accession number, title, authors, edition and publisher from a web page and store the information in a database and to search for a book with the title specified by the user and to display the search results with proper headings.
  738. ***
  739.  
  740. ### Procedure for execution:
  741.  
  742. * Open a terminal and run `mkdir rails_app && cd $_`
  743. * Create a new app called books using the command `rails new books -d mysql`
  744. * Run the command `cd books/config` and open the database.yml file to set mysql username and password by running the command `nano database.yml`. The *username* and *password* are required to be filled at 3 sections in the file.
  745. * The following is a sample snippet from the database.yml file-
  746.  
  747.     adapter: mysql2  
  748.     encoding: utf8  
  749.     reconnect: false  
  750.     database: books_development  
  751.     pool: 5
  752.     username: root  
  753.     password: pass  
  754.     socket: /var/run/mysqld/mysqld.sock
  755.  
  756. * Save the file by `ctrl+x` and then `y`.
  757. * Now run `cd ..` and make sure that you are present in the `rails_app/books` directory.
  758. * Run the command `rake db:create`
  759. * Next, run `rails generate controller books index`
  760. * Next, run `rails generate model book access_no:integer title:string author:string edition:integer
  761. publisher:string`
  762. * Run `rake db:migrate`
  763. * Now, `cd app/views/books` and edit the file `nano index.html.erb` -
  764.    
  765. * Also, edit the file `nano search.html.erb`
  766. * Now, `cd ../../controllers` and edit the file `nano books_controller.rb`
  767. * Next, run `cd ../../config` and edit `nano routes.rb` file.
  768. * Finally, run `cd ..` and start the rails server by running the command `rails s`
  769. * Open a browser and in the address bar type `localhost:3000` to check if the rails server is running. If yes, add to the URL, `localhost:3000/books/index`.
  770. * Enter the input & on successfully inserting data, search for the same from the browser. The output is displayed in a table on the browser.
  771.  
  772. ###Explaination
  773.  
  774. * The rails new command has created a Rails application in a new directory called books
  775. * The -d options in the rails new command allows us to specify which DBMS to use. The default is sqlite. However we use mysql
  776. * The database.yml file has all the cofiguration information which allows our application to connect, create and modify the database
  777. * You run rake db:create once and only once, and you run it first. Then you run rake db:migrate every time you add/change a migration
  778. * The rails generate controller command created a lot of files and directories. We are interested in two of them for now :-
  779.    * app/controllers/books_controller.rb
  780.    * app/views/books/index.html.erb
  781. * Inside Rails application, the controller file is placed inside app/controllers directory
  782. * ####The Controller
  783.    * User types a URL, lets say http://localhost:3000/pages/home. If the Rails server is running, the request first reaches the Rails router.
  784.    * ![Alt text](./l6_block_dia_vc.png)
  785.    
  786.    * The Router checks the config/routes.rb file to see if there is an entry matching the URL requested. In our case there is a route entry - get "pages/home". In short, the route matches incoming URL `/pages/home` to home action in Pages controller. So the request is forwarded to the Pages controller, home action.
  787.    
  788.    * ![Alt text](./l5_pages.png)
  789.  
  790.  
  791.    * In our program the BooksController is a class. Intially there is an empty method index inside the class. The method is also called index action in the Books controller.
  792.    
  793.                class BooksController < ApplicationController
  794.    
  795.                def index
  796.    
  797.                end
  798.  
  799.    * This action serves the url /books/index when accessed. Even though the index action is an empty method, it fetches the file /apps/views/books/index.html.erb and displays it on the browser.
  800.    
  801. * ####The Model
  802.    * Rails interact with the database through models. In our program we have a model called Book. This ensures that the our model is independent of the DBMS
  803.    * When you create a model, the name is singular and the name starts with capital letter. Book, instead of Books
  804.    * In our model we have the following attributes
  805.    
  806.        * access_no:integer
  807.        * title:string
  808.        * author:string
  809.        * edition:integer
  810.        * publisher:string
  811.        
  812.    * The `rails generate model` command also creates a migration file present under db/migrate
  813.    * The migration file is like schema which defines the database table structure
  814.    * rake db:migrate pushes the database changes from the migration file to the actual database
  815.    * If you end up doing a mistake you can always undo the creation of a model using
  816.          
  817.                   rails destroy model Book
  818.                
  819.    * If you want to delete the database from the DBMS once the model has been migrated use
  820.                    
  821.                    rake db:rollback
  822. * ####Ruby Tags
  823. * RoR has the following 2 tags :
  824.    * <% %> : The output of this tag will NOT be displayed on the browser
  825.    * <%= %> : The output will be displayed on the browser  
  826. ###Code:
  827. * **index.html.erb**
  828. ```erb
  829.    <h1>Books</h1><br>
  830.    <h3 style="text-align: center;">Add a book</h3><br>
  831.     <%= form_tag("/books/add", :method=>"post") do %>  
  832.     <form action="/books/add" method="post"> <br>
  833.     Access No:<%= text_field_tag(:b_access_no) %>      
  834.     //create input tags of given type and id
  835.     <input type="text" id=":b_access_no"><br>
  836.     <br>Title: <%= text_field_tag(:b_title) %>      
  837.     <input type="text" id=":b_title"><br>
  838.     <br>Author: <%= text_field_tag(:b_author) %>        
  839.     <input type="text" id=":b_author"><br>
  840.     <br>Edition: <%= text_field_tag(:b_edition) %>        
  841.     <input type="text" id=":b_edition"><br>
  842.     <br>Publisher: <%= text_field_tag(:b_publisher) %>  
  843.     <input type="text" id=":b_publisher"><br>
  844.     <br><br>
  845.     <%= submit_tag("Add Book") %>  
  846.     <input type="submit" value="Add Book"><br>
  847.     <% end %>
  848.     <br>
  849.     <h3 style="text-align: center;">Search for a book</h3><br>
  850.     <%= form_tag("/books/search", :method=>"post") do %>  
  851.     <form action="/books/search" method="post"><br>
  852.     Title: <%= text_field_tag(:bs_title) %>  
  853.     <input type="text" id=":bs_title"><br>
  854.     <br><br>
  855.     <%= submit_tag("Search") %>  
  856.     <input type="submit" value="Search"><br>
  857.     <% end %>  
  858.     <br>
  859. ```
  860.    
  861. * **search.html.erb**
  862. ```erb
  863.     <h1>Search Result</h1><br>
  864.     <table border=1><br>
  865.     <% @t=Book.find_by_title(params[:bs_title]) %>  
  866.     //RoR creates a find_by_attribute method for our model. In our case we are asked to search by title hence we are using the find_by_title function. This function takes a string as parameter. Here we are using the the value in :bs_title that was passed on using post method when the button was pressed. This function returns an array. Arrays in ruby begin with the '@' symbol
  867.     <br>
  868.     <tr><br>
  869.     <th>Access No.</th><br>
  870.     <th>Title</th><br>
  871.     <th>Author</th><br>
  872.     <th>Edition</th><br>
  873.     <th>Publisher</th><br>
  874.     </tr><br>
  875.     <tr><br>
  876.      //Elements of an array are accessed by the "." operator followed by the attribute name.
  877.      //In our case we are accessing the access_no that was returned to the array t.
  878.     <td> <%=  @t.access_no %> </td>  
  879.     <td> <%= @t.title %> </td>  
  880.     <td> <%= @t.author %> </td>  
  881.     <td> <%= @t.edition %> </td>  
  882.     <td> <%= @t.publisher %> </td>  
  883.     </tr><br>
  884.     </table><br>
  885.     <br><br><br>
  886.     <a href="/books/index">Back</a>  
  887.     <br>
  888. ```
  889. * **books_controller.rb**
  890. ```ruby
  891.     class BooksController < ApplicationController    
  892.    //auto-generated  code  
  893.    def index  
  894.    //auto-generated  code  
  895.    end  
  896.    //auto-generated  code<br>
  897.     def add  
  898.     //create a new action called add which performs the below action
  899.     Book.create(:access_no=>params[:b_access_no],:title=>params[:b_title],:author=>params[:b_author],:edition=> params[:b_edition],:publisher=> params[:b_publisher])  
  900.     //the create method is used to add a vlaue to the DB. The values to be added are fetched from the post method using the param function
  901.     redirect_to :action => 'index'  
  902.     //once the add button is clicked this specifes which page is to be displayed.Here the index page is redisplayed  
  903.     end  
  904.     def search  
  905.     //defines the search action. Just displayes search.html.erb on browser  
  906.     end  
  907.     end  
  908. ```
  909. * **routes.rb**
  910. ```ruby
  911.     Books::Application.routes.draw do  
  912.     //auto generated code  
  913.     match "books/index" => "books#index", :as => :index  
  914.     //use the action defined under index when request for /books/index is recieved<br>
  915.     match  "books/add" => "books#add", :via => :post  
  916.     //use action defined under add when request for books/add is recieved via post method<br>
  917.     match  "books/search" => "books#search", :via => :post  
  918.     //use action defined under search when request for books/search is recieved via post method<br>
  919. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement