Wednesday, September 24, 2008

observe_field Example - Ruby on Rails

<table>
<tr>
<td><label for="searchtext"><font size="1"><b>Live Book Search:</b></font></label></td>
<td><%= text_field_tag :searchtext %></td>
<td><img alt="spinner" id="spinner" src="/images/spinner.gif" style="display:none;" /></td>
</tr>
</table>

<%= observe_field 'searchtext', :frequency => 0.5,
:update => 'search_hits',
:loading => "Element.show('spinner')",
:complete => "Element.hide('spinner')",
:url => { :controller => 'books', :action=> 'live_search' },
:with => "'search=' + escape(value)" %>

<div id="search_hits">
<table>
<tr>
<th>Name</th>
<th>Author</th>
<th>Price</th>
<th>Desc</th>
</tr>

<% for book in @books %>
<tr>
<td><%=h book.name %></td>
<td><%=h book.author %></td>
<td><%=h book.price %></td>
<td><%=h book.desc %></td>
<td><%= link_to 'Show', book %></td>
<td><%= link_to 'Edit', edit_book_path(book) %></td>
<td><%= link_to 'Destroy', book, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>

For more detail go to
Wiki.rubyonrails.org

Variables In Request Env - Ruby on Rails

request.env is a Ruby Array that contains information about a visiting user’s and server environments.
request.env is accessible on all pages hosted in a ruby on rails site. The Array contains the following key/value pairs:

  • SERVER_NAME
  • PATH_INFO
  • REMOTE_HOST
  • HTTP_ACCEPT_ENCODING
  • HTTP_USER_AGENT
  • SERVER_PROTOCOL
  • HTTP_CACHE_CONTROL
  • HTTP_ACCEPT_LANGUAGE
  • HTTP_HOST
  • REMOTE_ADDR
  • SERVER_SOFTWARE
  • HTTP_KEEP_ALIVE
  • HTTP_REFERER
  • HTTP_COOKIE
  • HTTP_ACCEPT_CHARSET
  • REQUEST_URI
  • SERVER_PORT
  • GATEWAY_INTERFACE
  • QUERY_STRING
  • REMOTE_USER
  • HTTP_ACCEPT
  • REQUEST_METHOD
  • HTTP_CONNECTION
For testing purposes it might be useful to print out a table with the request.env information in it.

<table>
<th>key</th>
<th>Value</th>
<% for item in request.env %>
<tr>
<td><%= item[0] %></td>
<td><%= item[1] %></td>
</tr>
<% end %>
</table>

Installing Rmagick on Windows Using Ruby 1.8.6

Rmagick tends to be tricky to install, but on windows its relatively simple, except for the fact that if you freshly installed ruby 1.8.6, ruby gems needs to be updated. If you install ruby 1.8.6 and then go to ruby.forge and download rmagick for windows. Extract the zip folder, run the .exe, then open a command line, cd (change directory) to the install folder and do “gem install rmagick —local”. The first time you do this, you might run into the “Error installing gem RMagick (version) .gem[.gem]: buffer error”. To fix that run “gem update —system” with double hyphens and then run “gem install rmagick —local” and everything should now install fine.

Dynamically add and remove html elements using javascript

<input type="hidden" value="0" id="theValue" />
<p><a href="javascript:;" onclick="addElement();">Add Some Elements</a></p>
<div id="myDiv"> </div>

<script>
function addElement() {
var ni = document.getElementById('myDiv');
var numi = document.getElementById('theValue');
var num = (document.getElementById('theValue').value -1)+ 2;
numi.value = num;
var newdiv = document.createElement('div');
var divIdName = 'my'+num+'Div';
newdiv.setAttribute('id',divIdName);
newdiv.innerHTML = 'Element Number '+num+' has been added! <a href=\'#\' onclick=\'removeElement('+divIdName+')\'>Remove the div "'+divIdName+'"</a>';
ni.appendChild(newdiv);
}

function removeElement(divNum) {
var d = document.getElementById('myDiv');
var olddiv = document.getElementById(divNum);
d.removeChild(olddiv);
}
</script>

Contact Me for any help regarding rails/nodejs/php/mysql

Name

Email *

Message *