Request.QueryString + Select Redirect

Posted by John
on Thursday, 14 February 2008

Quick post, to handle parameters in your webpage via javascript, try this...

Request.QueryString with JS

Include the file in your header section then use like...

mypage.html?username=john

With the Javascript...

var name = '';
if (Request.QueryString("username").Count > 0) {
    name = Request.QueryString("username").Item(1);
}

The function returns the querystring as an object, so to determine whether you have any parameters returned count the number of objects. The first of which we want so we use .Item(1) to grab that and stick it in our variable name after which we can give to an alert() popup or whatever we need.

On Select Redirect with JS

Here's another bit, this will force the browser to redirect to another page once the user selects a value from the drop-down box and clicks the button image.

Javascript...

function goto(form) { 
var index=form.select.selectedIndex
    if (form.select.options[index].value.length != 0) {
        location.replace(form.select.options[index].value);
    }
}

Html...

<FORM NAME="form1">
<select name="select" id="select" style="width:90px;">
<option value="http://www.red91.com/">my site</option>
</select>
<img src="button.gif" onclick="goto(form1)" />
</FORM>

Rails Routes + Sharing Controllers

Posted by John
on Saturday, 28 July 2007

One of the key factors in developing a web app is to determine it’s site structure and how each section permeates to another.

Routes

When your user navigates to your web address, which controller/view is he going to see?. This is managed by Rails Routes which is an efficient upgrade from mod_rewrite rules in PHP.

to start…

go into your rails webapps root directory and open routes.rb in /config. this controls how the user perceives the webs look.

so scroll down till you see…
bq. map.connect ‘:controller/:action/:id.:format’
map.connect ‘:controller/:action/:id’

and above this you can add…
bq. map.connect ‘’, :controller => ’account’, :action => ‘signup’

make sure you rename or delete the index.html file in /public otherwise this won’t work.

now everytime someone goes to www.yours.com/ it’ll navigate them to the signup action of the account controller. this is rather than having www.yours.com/account/signup in the browser’s address bar.

of course, when they navigate away it’ll change to /account/list or whatever actions you’ve setup but at least now you can catch out users from jumping to the root and getting errors or redirecting them to the path manually.

note: the routes operate on precedence, so the one first will knock out the second, etc.

Sharing Controllers

when writing your app you may want to redirect an action in one view to an action in another view. e.g. click on the ‘posts’ link to view your ‘posts’; while your in the ‘accounts’ view/controller.

so to do this, add this inside your method for jumping to your posts

redirect_to :controller => ‘posts’, :action => ‘index’

find more at rails api