Forms in Ruby on Rails - Part 2
July 11th, 2007
In the previous installment, we learned about two of Rails’ methods that help create forms, form_for and remote_form_for. Those methods dealt with forms primarily based on Model data. Today we’ll turn our attention to form_tag and form_remote_tag which create more generic forms not tied to Model data.
Tutorial after the jump…
Read the rest of this entryForms in Ruby on Rails - Part 1
July 8th, 2007
I was confused for a pretty long time about how to create forms in Ruby on Rails. Since many (most?) Rails applications are tied to databases, it makes sense that many interactions with model data will be done via HTML forms. My problem though, was that there just seemed to be too many choices, and the choices are poorly named.
Tutorial after the jump…
Read the rest of this entryWindows is Evil
January 24th, 2007
Simple lesson for other Ruby on Rails noobs – editting dispatch.fcgi in Windows and then uploading to your website may be hazardous for your site!!
Although I haven’t had other issues with doing so, after totally destroying RubyNoob over the past few days, I’m always going to do any editting of dispatch.fcgi through my Dreamhost shell! Apparently some invisible characters can get into your file and keep your site from starting up.
I’ve also updated to the latest and greatest typo, but in the process I’ve lost the comments for the 4 or 5 most recent posts – and that sucks. I’ll see what I can do to resurrect them – or at least the ones worth bringing back.
I have an interesting article coming soon about Dreamhost and Ruby on Rails and Applications failing to start. I’m going to try to make the changes I’ll be mentioning and we’ll see if RubyNoob becomes more stable.
How to download files with a Ruby script
August 21st, 2006
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:
1 2 3 4 5 6 7 8 9 10 |
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) } } puts "Yay!!" |
The Net::HTTP class contains the magic needed to handle this operation. I don't think this will work at all if you need to pull down a file from an FTP server. For now, we're dealing with http urls. So, just strip off the "http://" portion of the url and everything after that up to the first / goes into the start method.
Now, we get the file. The rest of your image url after what you put in the start method goes into the get method. This grabs the file from flickr.
Now we're going to copy the file down to where the script is running. First, let's create the file we're going to copy the picture into. Using the open method, the first parameter is the name of the file that you're going to plop the picture into. We could have used "218926700_ecedc5fef7_o.jpg" or anything else here. The second parameter of the open method, "wb" indicates that we're opening the file for (w)riting and we're going to be writing (b)inary information. The "b" may not be necessary on non-Windows platforms.
Finally, we're going to write into the new file, the contents or "body" of what we grabbed from flickr. So, this writes in the binary bits of the picture into fun.jpg. Remember that with the way that we created fun.jpg it'll be in the same directory with our ruby script.
This same method will copy down .html files, .css files, .pdf's and just about any other kind of file. In my next RubyNoob entry, I'll write about how to combine this method with a flickr api call to grab an arbitrary number of recent photos from flickr. As usual, I'm still a noob, and there's probably much better ways to do this. If you know a better way, please share in the comment section below.
How to Send Files in Rails
May 19th, 2006
First, put the file out on your server. I put my "README.txt" file in a "files" folder like this: "rails/ appname/files/README.txt."
Next, set up a link in one of your pages:
1 2 |
<%= link_to 'Get Readme', :action => 'get_readme' %> |
1 2 3 4 |
def get_readme send_file("files/README.txt", :filename => 'yo_readme.txt') end |
There are other options like the :filename option which allow you to tell the browser what type of file to expect, how to display the file, and whether or not to stream the download. Streaming is turned on by default and allows downloading of very large files. All of these options go into the same line of code. Take a look at the article at this link.
As usual, expect a quick video demonstration of this posted later tonight.
Rails Custom URL Tutorial
May 18th, 2006
For instance, let's say that I get a list of my del.icio.us links tagged with the word "ruby." I edit them in whatever way I feel like, and then save my creation along with a keyword that I choose - let's say "ruby" for simplicity.
I don't want my users to have full access to the admin sections of my application - they'll only have access to create and update their query results. So, I want to give them the ability to use their keyword as part of the url. So it'll be something like http://delipaste.com/saved/ruby.
Read the rest of this entry
Strategy Design Pattern in Ruby
May 15th, 2006
Read the rest of this entry
Simple Rails RJS Tutorial
May 13th, 2006
Read the rest of this entry
Video of my Delicious App
May 9th, 2006
Please leave me a comment and let me know what you think!
Test
Scriptaclous Cheatsheet
April 28th, 2006
Awesome Scriptaculous CheatSheet(PDF)
How to add groovy Scriptaculous effects
April 25th, 2006
Read the rest of this entry
HowTo add Ajax in-progress indicators
April 24th, 2006
So, we need a way of letting the user know that something is happening and that they ought to wait a bit. Today, the standard seems to be an animated spinning icon:
Read the rest of this entry
RubyNoob gets all Ajaxy
April 23rd, 2006
"How I got some cool Ajax stuff to work with Ruby on Rails"
This weekend, my goal was to get my first original Ruby on Rails application working on my development box. I have achieved at least some sort of success! My app, such as it is, allows a user to enter a tag from her del.icio.ous account and then the app calls the del.icio.us REST API for that tag, strips out the html links and puts them into a text area. The user can then edit the text returned and save the tag/results combination into the database. It's not too exciting, but it's been a lot of fun to get together.
Read the rest of this entry
Rotating Ruby on Rails Log Files
April 20th, 2006
rake clear_logs
Now, back to the REST of the story:Read the rest of this entry
Quick Delicious API Ruby script
April 19th, 2006
My first task was just figuring out how to call one of the APIs through Ruby. After a lot of detective work, I figured out a first, very rough go at it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
require 'open-uri' require "rexml/document" include REXML user = "your_login" pass = "your_password" url='http://del.icio.us/api/posts/all?&tag=' puts 'Enter Tag: ' tag=gets print url+tag open(url+tag, :http_basic_authentication => [user, pass] ) do |f| result = f.read doc = Document.new result doc.elements.each("posts/post") {|element| puts element.attributes["href"] } end |
Read the rest of this entry