Using remote_function
This wonderful built-in function is a great way to make easy Ajax calls. In this example I will show you how to use remote_function with the onClick call, and also within a JavaScript function.
First, lets make a checkbox called finish; this link will send the id of the item to the database to be updated. The item will be a todo and we will be using the list.html.erb and the todo_controller.rb.
In the view named: list.html.erb
remote_function(:url => {:action => 'finish', :controller => 'todo',
:id => todo.id})
Now once the checkbox is clicked it will them make the Ajax call to the todo_controller.rb with the method finish.
In the controller: todo_controller.rb
def finish
@todo = Todo.update(params[:id], :status => '1')
if @todo.save
render :update do |page|
page.replace_html 'thedivID' + params[:id], 'Finished!'
end
end
end
The above code will first update the database setting the status from 0 to 1 (1 = finished and 0 = unfinished). Next we check if the update was successful, after that we render the update to the page. By using render :update, we can replace the html in side of the div or other element with the text ‘Finished’.
Note you can use remote_function in any way you see fit, ie: onclick, onload and others.
Passing another var with remote_function. lets say i want to pass the user id, list id and, the todo id. How would we do this with the remote function?
We know know the link should look like this: /todo/finish/17?todoid=72&userid=100, but how do we do this with rails? Below is how to do just that;
remote_function(:url => {:action => 'finish', :controller => 'todo',
:id => list.id, :todoid => todo.id, :userid => @user.id})
Winn Scriptatype: 1.7
I wanted to re-cap the new adds in 1.7 for rails. The new method calls are as listed:
# Added V1.7b
textlink_do_fade
textlink_do_puff
textlink_do_blindup
textlink_do_dropoutYou can build any of the above calls with:
textlink_do_fade('Link Text','divYouWantToUpdate',{***},{****})
Visit the project for more info on how to use the latest methods and more. Winn Scriptatype project home page. at Ruby On Rails Railway.
WinnScriptatype:Rails v1.7b
I just released WinnScriptatype version 1.7 beta on the RubyForge project page. I have also made some fixes to version 1.5 and it is now production ready. The latest release has added some new functionality and the ability to make calls to display text. The call still supports the effects and Ajax request url along with url options.
[ruby]
# New calls in 1.7b
textlink_do_fade
textlink_do_puff
textlink_do_blindup
textlink_do_dropout
[/ruby]
I expect i will be making the same update to the PHP version soon so keep an eye out for that!
Take a look at the WinnScriptatype:Rails RubyForge project page and download the latest!
Rails Plugin: WinnScriptatype
Making an Ajax checkbox just got easy. This very simple plugin was built for my own use. The idea was to make one call for my checkbox, update to the database, and an effect. This plugin is easy to use and install, i dont have it setup in an svn yet so you will need to download it and place it in RAILS_ROOT/vendor/plugins/HERE.
I had been working on this plugin for a few weeks in making a new application. I kept writing the “input, Ajax.Request and, Effect.Fade”, so i needed a way to make that fast and do what i need. And out came this handy plugin!
[ruby]
# Lets use this new plugin
checkbox_do_fade(‘theDiv’, ‘CheckboxID’, {:controller => ‘todo’, :action => ‘finish’, :id => todo.id}, {:userid => user.id})
[/ruby]
Here is an over view of the options:
[ruby]
checkbox_do_fade(‘*’, ‘**’, {***}, {****})
[Element ID]* This is the name of the element you want to effect such as a div or table (tr,td)…
[Checkbox ID]** This is the id of the checkbox it self
[URL/path]*** this is the url for the update to take place NOTE: always use controller,
action, and id DO NOT place other variables in this block that is for the next block.
[URL/path options]**** This block is used for url options like so: (/path/to/1?userid=1) as you see
userid is 1, this is done by using the url options block. You may send an unlimited number of
url options with this.
# Below are the ONLY supported calls
checkbox_do_fade # This will fade the element
checkbox_do_puff # This will puff the element
checkbox_do_blindup # and so on…
checkbox_do_dropout # again…
[/ruby]
Ready to start using it? Download WinnScriptatype
You will also need help installing this plugin so take a look at Ruby on Rails Railway : WinnScriptatype.
Magic Multi-Connections
This plug-in is a great one! I have found a few bugs but it did the job just as i needed it to. I am using this plug-in to connect to three databases, where i am selecting, updating, inserting, and deleting. By adding this plug-in i was able to set my databases in the database.yml file and name them as i wanted. Next i adding two modules to my environment.rb file. After that i was able to connect to each of the databases with ease.
Below is how my database.yml file looks:
[ruby]
databaseone:
adapter: mysql
database: one
username: root
password:
host: localhost
databasetwo:
adapter: mysql
database: two
username: root
password:
host: localhost
production:
adapter: mysql
database: three
username: root
password:
host: localhost
[/ruby]
Now in my environment.rb file i have:
[ruby]
module Dbone
establish_connection :databaseone
end
module Dbtwo
establish_connection :databasetwo
end
[/ruby]
Now if i would like to select from my database one i do the following in any controller:
[ruby]
def index
@users = Dbone::Users.find(:all)
end
[/ruby]
Get the idea now? It’s the same to update and add to the database. Just use the module name. Now for the “production” database everything remains the same, no need to add a module to it.
visual_effect with a delay
One of the coolest things I ran across in RJS was the delay method. This delay method will do just that and delay a number of seconds. So lets say you need to highlight something that was just added to the page after the user submits it. I would like it to add to the page then wait 1 second and then highlight. The following is the code for that.
[ruby]
render :update do |page|
page.html_replace ‘testdiv’, params[:the_text_added]
page.delay(1) do
page.visual_effect :highlight, ‘testdiv’, :duration => 2
end
end
[/ruby]
Cool huh? Try it out and see what you can do with delay()!
Highlight your UI with rails
If you find yourself needing to highlight something after your Ajax call comes back and is finished. Why not highlight it? If your making a call with remote_function, link_to_remote or other then add this small line of code on. Problem solved!
[ruby]
:complete => visual_effect(:highlight, ‘theDivId’)
[/ruby]
Just give it the div or element id and it works like a charm!
Don’t know how to use remote_function? Check out this post: Using remote_function
Listing files in a directory with Rails

In a recent application I am building I needed to pull file names and directory names from a local directory. I searched all over the net for this and found nothing. After reviewing a few of rails built in functions I came across Dir.glob(). If you feed this method a path to a directory it will then come back with an array of the names and directories listed.
So lets say I am going to tell Dir.glob to look in my “files” folder. The path I need to feed it is starting from the root of my app so the path would be “public/files/”.
1 | @files = Dir.glob("public/files/*") |
I added “*” so I can pull all files and dir’s back. If I just want to pull PDF’s back I can use this:
1 | @files = Dir.glob("public/files/*.pdf") |
That will show all PDF’s in that folder. You may also give it a file name and just pull one file out.
Anyway back to example one, I am looking for all files and all directories in the folder “files”. Below is the array I get back from @files.
1 2 3 4 5 6 7 8 9 10 11 | for file in @files puts file end #==== Output is: public/files/test1.gif public/files/test2.gif public/files/folder |
This is the best way I have found for pulling files from a directory and listing them in an array.
How to download files with a Ruby
For a project that I’m working on, I want to download a set of pictures from flickr once a day. In this way, my Rails app won’t need to access the flickr api – it can just grab the pics out of an images directory.
I figured that it would be an awfully easy task to download pictures from the internet using Ruby, and it is, but it was very difficult to find good examples. I googled for an hour or so, and kept coming up empty-handed. Finally, I checked in gmail where I keep mail from the Ruby Language mailing list. I found a relatively straightfoward way to do this.
Let’s say that you want to download a picture from flickr, and you know its URL. Here’s the ruby script to download the file:
[ruby]
require ‘net/http’
Net::HTTP.start(“static.flickr.com”) { |http|
resp = http.get(“/92/218926700_ecedc5fef7_o.jpg”)
open(“fun.jpg”, “wb”) { |file|
file.write(resp.body)
}
}
[/ruby]
Practical security on your application
Developing online applications is a way of life for a lot of programmers! It’s a great feeling working your but off day in and day out, night in and night out, sometimes all night and sometimes with no brakes! Then launch day comes and you are so happy any cant wait for people to start using your application! You launch your application without a hitch, all goes well!
Keeping that application secure is important, so when do you start worrying about security? I say right away, start building your application with maximum security. Keep track of what cookies you are setting as they can be in danger from a JavaScript injection.
So if your application has to use cookies how should you authenticate the user? I find writing a session id to the database and to a cookie along with a username or email, this process becomes very easy and secure. So how does this work?
Start with a login or registration, and then start the session. Pull the session id into a variable the send it to the database. After that write the session id to a cookie.
[php]
session_start();
$sid = session_id();
// your SQL insert or update “UPDATE accounts SET sid=’$sid’ WHERE id=1″
setcookie(‘appname_session’, $sid, time()+3600);
[/php]
After setting the session id cookie we need to set the username or email cookie!
[php]
setcookie(‘appname_user’, $username, time()+3600);
[/php]
Now you are able to authenticate the user inside your application securely.
[php]
// Select the user now
find_by_username_and_sid(cookie(“appname_user”),cookie(“appname_session”));
[/php]
This is in PHP but the same idea applies in all languages.
Categories
Pages
- contact
- Contact Form V1.8
- FlickrHelper
- Guestbook V1.01 ASP
- Guestbook V2.4.7
- iFrame Loader
- Scriptatype:PHP
- Winn Cal
- Winn Tracker
