Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 23.29 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>Stacking</title>
  148.         <script type = "text/javascript">
  149.         function bringThisUp(div1,div2,div3)
  150.         {
  151.             document.getElementById("paragraph1").style.zIndex = div1;
  152.             document.getElementById("paragraph2").style.zIndex = div2;
  153.             document.getElementById("paragraph3").style.zIndex = div3;
  154.         }
  155.         </script>
  156.         <style type ="text/css">
  157.         .paragraph
  158.         {
  159.             border: solid 4px;
  160.             width:4in;
  161.             height:3in;
  162.             position:absolute;
  163.         }
  164.         #paragraph1
  165.         {
  166.             left:5in;
  167.             top:1.5in;
  168.             zIndex:0;
  169.             border-color:black;
  170.             background-color:red;
  171.         }
  172.         #paragraph2
  173.         {
  174.             left:6in;
  175.             top:2in;
  176.             zIndex:1;
  177.             border-color:black;
  178.             background-color:blue;
  179.         }
  180.         #paragraph3
  181.         {
  182.             left:7in;
  183.             top:2.5in;
  184.             zIndex:2;
  185.             border-color:black;
  186.             background-color:green;
  187.         }
  188.         </style>
  189.     </head>
  190.     <body bgcolor="white" text = "black">
  191.     <h1>
  192.     <div id = "paragraph1" class = "paragraph" onmouseover = "bringThisUp(2,0,1)" align = "center"><br /><br />
  193.     This is the first paragraph
  194.     </div>
  195.     <div id = "paragraph2" class = "paragraph" onmouseover = "bringThisUp(0,2,1)" align = "center"><br /><br />
  196.     This is the second paragraph
  197.     </div>
  198.     <div id = "paragraph3" class = "paragraph" onmouseover = "bringThisUp(0,1,2)" align = "center"><br /><br />
  199.     This is the third paragraph
  200.     </div>
  201.     </h1>
  202.     </body>
  203. </html>
  204.  
  205.  
  206.  
  207. 3b
  208.  
  209.  
  210.  
  211. <html>
  212.     <head>
  213.         <title>Stacking</title>
  214.         <script type = "text/javascript">
  215.         function bringThisUp(currDiv)
  216.         {
  217.             var myDiv = document.getElementById(currDiv);
  218.             currIndex=myDiv.style.zIndex;
  219.             myDiv.style.zIndex="3";
  220.         }
  221.         function sendThisBack(currDiv)
  222.         {
  223.             var myDiv = document.getElementById(currDiv);
  224.             myDiv.style.zIndex=currIndex;
  225.         }
  226.         </script>
  227.         <style type ="text/css">
  228.         .paragraph
  229.         {
  230.             border: solid 4px;
  231.             width:4in;
  232.             height:3in;
  233.             position:absolute;
  234.         }
  235.         #paragraph1
  236.         {
  237.             left:5in;
  238.             top:1.5in;
  239.             zIndex:0;
  240.             border-color:black;
  241.             background-color:red;
  242.         }
  243.         #paragraph2
  244.         {
  245.             left:6in;
  246.             top:2in;
  247.             zIndex:1;
  248.             border-color:black;
  249.             background-color:blue;
  250.         }
  251.         #paragraph3
  252.         {
  253.             left:7in;
  254.             top:2.5in;
  255.             zIndex:2;
  256.             border-color:black;
  257.             background-color:green;
  258.         }
  259.         </style>
  260.     </head>
  261.     <body bgcolor="white" text = "black">
  262.     <h1>
  263.     <div id = "paragraph1" class = "paragraph" onmouseover = "bringThisUp(this.id)" onmouseout = "sendThisBack(this.id)" align = "center"><br /><br />
  264.     This is the first paragraph
  265.     </div>
  266. <div id = "paragraph2" class = "paragraph" onmouseover = "bringThisUp(this.id)" onmouseout = "sendThisBack(this.id)" align = "center"><br /><br />
  267.     This is the second paragraph
  268.     </div>
  269. <div id = "paragraph3" class = "paragraph" onmouseover = "bringThisUp(this.id)" onmouseout = "sendThisBack(this.id)" align = "center"><br /><br />
  270.     This is the third paragraph
  271.     </div>
  272.     </h1>
  273.     </body>
  274. </html>
  275.  
  276.  
  277.  
  278. 4a.xml
  279.  
  280.  
  281. <?xml version = "1.0"?>
  282. <?xml-stylesheet type="text/css" href="4a.css"?>
  283. <VTU>
  284.     <student>
  285.         STUDENT INFO
  286.         <USN>1JT15CS001</USN>
  287.         <name>Arun</name>
  288.         <college>JIT</college>
  289.         <branch>CSE</branch>
  290.         <YOJ>2015</YOJ>
  291.         <email>arun@gmail.com</email>
  292.     </student>
  293.     <student>
  294.         <USN>1JT15CS002</USN>
  295.         <name>Gopal</name>
  296.         <college>JIT</college>
  297.         <branch>CSE</branch>
  298.         <YOJ>2015</YOJ>
  299.         <email>gopal@gmail.com</email>
  300.     </student>
  301.     <student>
  302.         <USN>1JT15CS003</USN>
  303.         <name>Sanjay</name>
  304.         <college>JIT</college>
  305.         <branch>CSE</branch>
  306.         <YOJ>2015</YOJ>
  307.         <email>san@gmail.com</email>
  308.     </student>
  309. </VTU>
  310.  
  311.  
  312.  
  313. 4a.css
  314.  
  315. USN
  316. {
  317.     display:block;
  318.     color:blue;
  319.     font-style:italic;
  320. }
  321. college
  322. {
  323.     display:block;
  324.     color:green;
  325.     font-style:italic;
  326. }
  327. branch
  328. {
  329.     display:block;
  330.     color:red;
  331.     font-style:italic;
  332. }
  333. email
  334. {
  335.     display:block;
  336.     color:blue;
  337.     font-style:italic;
  338. }
  339.  
  340.  
  341.  
  342. 4b.xml
  343.  
  344.  
  345. <?xml version = "1.0" encoding="ISO-8889-1"?>
  346. <?xml-stylesheet type="text/xsl" href="4b.xsl"?>
  347. <VTU>
  348.     <student>
  349.         <name>Arun</name>
  350.         <usn>1JT15CS001</usn>
  351.         <college>JIT</college>
  352.         <branch>CSE</branch>
  353.         <year>2015</year>
  354.         <email>arun@gmail.com</email>
  355.     </student>
  356.     <student>
  357.         <name>Gopal</name>
  358.         <usn>1JT15CS002</usn>
  359.         <college>JIT</college>
  360.         <branch>CSE</branch>
  361.         <year>2015</year>
  362.         <email>gopal@gmail.com</email>
  363.     </student>
  364.     <student>
  365.         <name>Sanjay</name>
  366.         <usn>1JT15CS003</usn>
  367.         <college>JIT</college>
  368.         <branch>CSE</branch>
  369.         <year>2015</year>
  370.         <email>san@gmail.com</email>
  371.     </student>
  372. </VTU>
  373.  
  374.  
  375.  
  376. 4b.xsl
  377.  
  378.  
  379. <?xml version = "1.0" encoding="ISO-8889-1"?>
  380. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  381. <xsl:template match="/">
  382. <html>
  383.     <body>
  384.         <h2>VTU Student Information</h2>
  385.         <table border="1">
  386.             <tr bgcolor="#99cd32">
  387.                 <th>name</th>
  388.                 <th>usn</th>
  389.                 <th>collegeName</th>
  390.                 <th>branch</th>
  391.                 <th>year</th>
  392.                 <th>email</th>
  393.             </tr>
  394.         <xsl:for-each select="VTU/student">
  395.         <xsl:choose>
  396.             <xsl:when test="name='Gopal'">
  397.             <tr bgcolor="#ff00ff">
  398.                 <td><xsl:value-of select="name"/></td>
  399.                 <td><xsl:value-of select="usn"/></td>
  400.                 <td><xsl:value-of select="college"/></td>
  401.                 <td><xsl:value-of select="branch"/></td>
  402.                 <td><xsl:value-of select="year"/></td>
  403.                 <td><xsl:value-of select="email"/></td>
  404.             </tr>
  405.             </xsl:when>
  406.             <xsl:otherwise>
  407.             <tr>
  408.                 <td><xsl:value-of select="name"/></td>
  409.                 <td><xsl:value-of select="usn"/></td>
  410.                 <td><xsl:value-of select="college"/></td>
  411.                 <td><xsl:value-of select="branch"/></td>
  412.                 <td><xsl:value-of select="year"/></td>
  413.                 <td><xsl:value-of select="email"/></td>
  414.             </tr>
  415.             </xsl:otherwise>
  416.         </xsl:choose>
  417.         </xsl:for-each>
  418.         </table>
  419.         <h2>Selected student is highlighted</h2>
  420.     </body>
  421. </html>
  422. </xsl:template>
  423. </xsl:stylesheet>
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430. 5a.pl
  431.  
  432.  
  433. #! /usr/bin/perl
  434.  
  435. use CGI ':standard';
  436.  
  437. print header;
  438. print start_html("Lab 5a");
  439. print "SERVER INFORMATION <br/><br/>";
  440. print "Server Name = $ENV{'SERVER_NAME'} <br/><br/>";
  441. print "Server Software = $ENV{'SERVER_SOFTWARE'}<br/><br/>";
  442. print "Server Protocol = $ENV{'SERVER_PROTOCOL'}<br/><br/>";
  443. print "CGI Revision = $ENV{'GATEWAY_INTERFACE'}<br/><br/>";
  444. print end_html;
  445.  
  446.  
  447.  
  448. 5b.html
  449.  
  450. <html>
  451.     <head>
  452.         <title>Lab 5b</title>
  453.     </head>
  454.     <body>
  455.         <form action ="http://localhost/cgi-bin/5b.pl" method="POST">
  456.         <h3>Enter the Unix command!</h3>
  457.         <input type = "text" name = "cmd"/>
  458.         <input type ="submit" value = "Execute"/>
  459.         </form>
  460.     </body>
  461. </html>
  462.  
  463.  
  464.  
  465. 5b.pl
  466.  
  467. #! /usr/bin/perl
  468.  
  469. use CGI':standard';
  470.  
  471. print header;
  472. print start_html("Lab 5b");
  473. $c=param('cmd');
  474. system($c);
  475. print end_html;
  476.  
  477.  
  478. 6a.html
  479.  
  480.  
  481. <html>
  482. <head><title>Lab 6a</title></head>
  483. <body>
  484. <form action = "http://localhost/cgi-bin/6a.pl">
  485. <label>Enter Name: <input type="text" name="usrname"/></label>
  486. <input type = "submit"/>
  487. </form>
  488. </body>
  489. </html>
  490.  
  491.  
  492. 6a.pl
  493.  
  494.  
  495. #! /usr/bin/perl
  496. use CGI':standard';
  497. print header;
  498. print start_html("Lab 6a");
  499. $n = param('usrname');
  500. $range = 4;
  501. $a = int(rand($range));
  502. if($a == 0)
  503. {
  504. print "Hi ";
  505. }
  506. elsif($a == 1)
  507. {
  508. print "Hello ";
  509. }
  510. elsif($a == 2)
  511. {
  512. print "Bye ";
  513. }
  514. else
  515. {
  516. print "How are u? "
  517. }
  518. print $n;
  519. print end_html;
  520.  
  521.  
  522.  
  523.  
  524. 6b.pl
  525.  
  526. #! /usr/bin/perl
  527. use CGI':standard';
  528. print header;
  529. print start_html("Lab 6b");
  530. open(FILE,'<count.txt');
  531. $a = <FILE>;
  532. $a++;
  533. close(FILE);
  534. open(FILE,'>count.txt');
  535. print FILE "$a";
  536. close(FILE);
  537. print h1("Number of visiters are $a");
  538. print end_html;
  539.  
  540.  
  541. 7.pl
  542.  
  543.  
  544.  
  545. #! /usr/bin/perl
  546. use CGI':standard';
  547. ($s,$m,$h) = localtime(time);
  548. $delay = 1;
  549. print "Refresh: ", $delay, "\n";
  550. print header;
  551. print start_html("Lab 7");
  552. print "Time = $h:$m:$s";
  553. print end_html;
  554.  
  555.  
  556. 8.html
  557.  
  558.  
  559. <?xml version="1.0" encoding="UTF-8"?
  560. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  561. "http://www/w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  562.  
  563. <html xmlns="http://www.w3.org/1999/xhtml">
  564.     <body>
  565.         <form action="http://localhost/cgi-bin/8.pl" method="post">
  566.             <table>
  567.                 <tr>
  568.                     <td>Enter your name </td> <td><input type="text" name="name"></td>
  569.                 </tr>
  570.                     <td>Enter your age </td> <td><input type="text" name="age"></td>
  571.                 </tr>
  572.                 <tr>
  573.                     <td>Click here to send </td> <td><input type="submit" value="Send"></td>
  574.                 </tr>
  575.             </table>
  576.         </form>
  577.     </body>
  578. </html>
  579.  
  580.  
  581.  
  582.  
  583. 8.pl
  584.  
  585.  
  586. #! /usr/bin/perl
  587. print "Content-type: text/html\n\n";
  588. print "<HTML><TITLE>Result of the insert operation </TITLE>";
  589. use CGI ':standard';
  590. use DBI;
  591. $dbh=DBI->connect("DBI:mysql:db8","root","jit123");
  592. $name=param("name");
  593. $age=param("age");
  594. $qh=$dbh->prepare("insert into tb values('$name','$age')");
  595. $qh->execute();
  596. $qh=$dbh->prepare("select * from tb");
  597. $qh->execute();
  598. print "<table border size=1><tr><th>Name</th><th>Age</th></tr>";
  599. while ( ($name,$age)=$qh->fetchrow())
  600. {
  601.        print "<tr><td>$name</td><td>$age</td></tr>";
  602. }
  603. print "</table>";
  604. $qh->finish();
  605. $dbh->disconnect();
  606. print"</HTML>";
  607.  
  608.  
  609.  
  610. 9.php
  611.  
  612.  
  613. <?php
  614. $inTwoMonths = 60*60*24*60+time();
  615. setcookie('lastVisit',date("h:i:s A - m/d/y"), $inTwoMonths);
  616. if(isset($_COOKIE['lastVisit']))
  617.     $visit=$_COOKIE['lastVisit'];
  618. else
  619.     echo "You've got some expired cookies!";
  620. echo "Your last visit was - ". $visit;
  621. ?>
  622.  
  623.  
  624.  
  625. 10.php
  626.  
  627. <?php
  628. session_start();
  629. if(isset($_SESSION['views']))
  630.     $_SESSION['views']=$_SESSION['views']+1;
  631. else
  632.     $_SESSION['views']=1;
  633. echo "Page views = ".$_SESSION['views'];
  634. ?>
  635.  
  636.  
  637. 11.html
  638.  
  639.  
  640. <html>
  641. <head>
  642.     <title>HTML, PHP and MySQL</title>
  643. </head>
  644. <body>
  645.     <h3>    Enter information</h3>
  646.     <form action="http://localhost/akshay11a.php" method="POST">
  647.         <pre>
  648. Name
  649. <input type="text" name="name" />
  650.  
  651. Address 1
  652. <input type="text" name="address1" />
  653.  
  654. Address 2
  655. <input type="text" name="address2" />
  656.  
  657. E-mail
  658. <input type="text" name="email" />
  659.  
  660. <input type="submit" value="Submit" />  <input type="reset" value="Clear" />
  661.         </pre>
  662.     </form>
  663.    
  664.     <form action="http://localhost/akshay11b.php" method="POST">
  665.         <pre>
  666. Enter the name
  667. <input type="text" name="name" />
  668.  
  669. <input type="submit" value="Submit" />
  670.         </pre>
  671.     </form>
  672. </body>
  673. </html>
  674.  
  675.  
  676.  
  677.  
  678. 11a.php
  679.  
  680.  
  681.  
  682. <?php
  683.  
  684. $name=htmlspecialchars($_POST['name']);
  685. $address1=htmlspecialchars($_POST['address1']);
  686. $address2=htmlspecialchars($_POST['address2']);
  687. $email=htmlspecialchars($_POST['email']);
  688.  
  689. $connection = mysql_connect("localhost", "root", "jit123") or die(mysql_error());
  690. mysql_select_db("akshay") or die(mysql_error());
  691. $sql = "insert into user(name, addr1, addr2, email) values ( '$name', '$address1', '$address2', '$email');";
  692.  
  693. mysql_query($sql) or die(mysql_error);
  694. echo "Values inserted";
  695.  
  696. ?>
  697.  
  698.  
  699. 11b.php
  700.  
  701.  
  702. <html>
  703. <body>
  704.  
  705.  
  706. <?php
  707.  
  708. $name=htmlspecialchars($_POST['name']);
  709.  
  710. $connection = mysql_connect("localhost", "root", "jit123") or die(mysql_error());
  711. mysql_select_db("akshay") or die(mysql_error());
  712. $sql = "select * from user where name like '$name%';";
  713.  
  714. $result = mysql_query($sql) or die(mysql_error);
  715. ?>
  716.  
  717. <table border="1">
  718. <tr><th>Name</th><th>Address 1</th><th>Address 2</th><th>E-Mail</th></tr>
  719.  
  720. <?php
  721.  
  722. while($cols=mysql_fetch_row($result))
  723. {
  724.    echo "<tr>
  725.             <td>$cols[0]</td>
  726.             <td>$cols[1]</td>
  727.             <td>$cols[2]</td>
  728.             <td>$cols[3]</td>
  729.             </tr>";
  730. }
  731.  
  732. ?>
  733. </table>
  734. </body>
  735. </html>
  736.  
  737.  
  738.  
  739.  
  740. 12
  741.  
  742.  
  743.  
  744.  
  745. ## Program 12:
  746. ### 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.
  747. ***
  748.  
  749. ### Procedure for execution:
  750.  
  751. * Open a terminal and run `mkdir rails_app && cd $_`
  752. * Create a new app called books using the command `rails new books -d mysql`
  753. * 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.
  754. * The following is a sample snippet from the database.yml file-
  755.  
  756.     adapter: mysql2  
  757.     encoding: utf8  
  758.     reconnect: false  
  759.     database: books_development  
  760.     pool: 5
  761.     username: root  
  762.     password: pass  
  763.     socket: /var/run/mysqld/mysqld.sock
  764.  
  765. * Save the file by `ctrl+x` and then `y`.
  766. * Now run `cd ..` and make sure that you are present in the `rails_app/books` directory.
  767. * Run the command `rake db:create`
  768. * Next, run `rails generate controller books index`
  769. * Next, run `rails generate model book access_no:integer title:string author:string edition:integer
  770. publisher:string`
  771. * Run `rake db:migrate`
  772. * Now, `cd app/views/books` and edit the file `nano index.html.erb` -
  773.    
  774. * Also, edit the file `nano search.html.erb`
  775. * Now, `cd ../../controllers` and edit the file `nano books_controller.rb`
  776. * Next, run `cd ../../config` and edit `nano routes.rb` file.
  777. * Finally, run `cd ..` and start the rails server by running the command `rails s`
  778. * 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`.
  779. * Enter the input & on successfully inserting data, search for the same from the browser. The output is displayed in a table on the browser.
  780.  
  781. ###Explaination
  782.  
  783. * The rails new command has created a Rails application in a new directory called books
  784. * The -d options in the rails new command allows us to specify which DBMS to use. The default is sqlite. However we use mysql
  785. * The database.yml file has all the cofiguration information which allows our application to connect, create and modify the database
  786. * 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
  787. * The rails generate controller command created a lot of files and directories. We are interested in two of them for now :-
  788.    * app/controllers/books_controller.rb
  789.    * app/views/books/index.html.erb
  790. * Inside Rails application, the controller file is placed inside app/controllers directory
  791. * ####The Controller
  792.    * User types a URL, lets say http://localhost:3000/pages/home. If the Rails server is running, the request first reaches the Rails router.
  793.    * ![Alt text](./l6_block_dia_vc.png)
  794.    
  795.    * 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.
  796.    
  797.    * ![Alt text](./l5_pages.png)
  798.  
  799.  
  800.    * 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.
  801.    
  802.                class BooksController < ApplicationController
  803.    
  804.                def index
  805.    
  806.                end
  807.  
  808.    * 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.
  809.    
  810. * ####The Model
  811.    * 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
  812.    * When you create a model, the name is singular and the name starts with capital letter. Book, instead of Books
  813.    * In our model we have the following attributes
  814.    
  815.        * access_no:integer
  816.        * title:string
  817.        * author:string
  818.        * edition:integer
  819.        * publisher:string
  820.        
  821.    * The `rails generate model` command also creates a migration file present under db/migrate
  822.    * The migration file is like schema which defines the database table structure
  823.    * rake db:migrate pushes the database changes from the migration file to the actual database
  824.    * If you end up doing a mistake you can always undo the creation of a model using
  825.          
  826.                   rails destroy model Book
  827.                
  828.    * If you want to delete the database from the DBMS once the model has been migrated use
  829.                    
  830.                    rake db:rollback
  831. * ####Ruby Tags
  832. * RoR has the following 2 tags :
  833.    * <% %> : The output of this tag will NOT be displayed on the browser
  834.    * <%= %> : The output will be displayed on the browser  
  835. ###Code:
  836. * **index.html.erb**
  837. ```erb
  838.    <h1>Books</h1><br>
  839.    <h3 style="text-align: center;">Add a book</h3><br>
  840.     <%= form_tag("/books/add", :method=>"post") do %>  
  841.     <form action="/books/add" method="post"> <br>
  842.     Access No:<%= text_field_tag(:b_access_no) %>      
  843.     //create input tags of given type and id
  844.     <input type="text" id=":b_access_no"><br>
  845.     <br>Title: <%= text_field_tag(:b_title) %>      
  846.     <input type="text" id=":b_title"><br>
  847.     <br>Author: <%= text_field_tag(:b_author) %>        
  848.     <input type="text" id=":b_author"><br>
  849.     <br>Edition: <%= text_field_tag(:b_edition) %>        
  850.     <input type="text" id=":b_edition"><br>
  851.     <br>Publisher: <%= text_field_tag(:b_publisher) %>  
  852.     <input type="text" id=":b_publisher"><br>
  853.     <br><br>
  854.     <%= submit_tag("Add Book") %>  
  855.     <input type="submit" value="Add Book"><br>
  856.     <% end %>
  857.     <br>
  858.     <h3 style="text-align: center;">Search for a book</h3><br>
  859.     <%= form_tag("/books/search", :method=>"post") do %>  
  860.     <form action="/books/search" method="post"><br>
  861.     Title: <%= text_field_tag(:bs_title) %>  
  862.     <input type="text" id=":bs_title"><br>
  863.     <br><br>
  864.     <%= submit_tag("Search") %>  
  865.     <input type="submit" value="Search"><br>
  866.     <% end %>  
  867.     <br>
  868. ```
  869.    
  870. * **search.html.erb**
  871. ```erb
  872.     <h1>Search Result</h1><br>
  873.     <table border=1><br>
  874.     <% @t=Book.find_by_title(params[:bs_title]) %>  
  875.     //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
  876.     <br>
  877.     <tr><br>
  878.     <th>Access No.</th><br>
  879.     <th>Title</th><br>
  880.     <th>Author</th><br>
  881.     <th>Edition</th><br>
  882.     <th>Publisher</th><br>
  883.     </tr><br>
  884.     <tr><br>
  885.      //Elements of an array are accessed by the "." operator followed by the attribute name.
  886.      //In our case we are accessing the access_no that was returned to the array t.
  887.     <td> <%=  @t.access_no %> </td>  
  888.     <td> <%= @t.title %> </td>  
  889.     <td> <%= @t.author %> </td>  
  890.     <td> <%= @t.edition %> </td>  
  891.     <td> <%= @t.publisher %> </td>  
  892.     </tr><br>
  893.     </table><br>
  894.     <br><br><br>
  895.     <a href="/books/index">Back</a>  
  896.     <br>
  897. ```
  898. * **books_controller.rb**
  899. ```ruby
  900.     class BooksController < ApplicationController    
  901.    //auto-generated  code  
  902.    def index  
  903.    //auto-generated  code  
  904.    end  
  905.    //auto-generated  code<br>
  906.     def add  
  907.     //create a new action called add which performs the below action
  908.     Book.create(:access_no=>params[:b_access_no],:title=>params[:b_title],:author=>params[:b_author],:edition=> params[:b_edition],:publisher=> params[:b_publisher])  
  909.     //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
  910.     redirect_to :action => 'index'  
  911.     //once the add button is clicked this specifes which page is to be displayed.Here the index page is redisplayed  
  912.     end  
  913.     def search  
  914.     //defines the search action. Just displayes search.html.erb on browser  
  915.     end  
  916.     end  
  917. ```
  918. * **routes.rb**
  919. ```ruby
  920.     Books::Application.routes.draw do  
  921.     //auto generated code  
  922.     match "books/index" => "books#index", :as => :index  
  923.     //use the action defined under index when request for /books/index is recieved<br>
  924.     match  "books/add" => "books#add", :via => :post  
  925.     //use action defined under add when request for books/add is recieved via post method<br>
  926.     match  "books/search" => "books#search", :via => :post  
  927.     //use action defined under search when request for books/search is recieved via post method<br>
  928. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement