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 entry

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 entry

Windows 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.

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:

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.
Setting up a link in your Rails app to allow your users to download any file is incredibly easy. Not just incredibly easy - OMGWTHBBQ easy! Stuff like this makes Rails so cool!

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' %>
Finally, add the "get_readme" action method to your controller:
1
2
3
4

  def get_readme
    send_file("files/README.txt", :filename => 'yo_readme.txt')
  end
That's it! Now, whenever a user clicks the "Get Readme" link on your page, the file will be sent to the client. The :filename option simply allows you to suggest a name for the file to your users. I threw it in just to see how it works. I couldn't believe it was this easy, but sure enough, it is. Very cool.

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

When my del.icio.us rails app finally goes live, one of the features it will have will be the ability for users to associate a retreived query with a keyword so that they can easily get back to it in the future.

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
As I'm reading through Head First Design Patterns, I've decided to write some sample ruby code for each pattern. This will certainly be educational for me, and it might prove useful for some of my readers. As always, I am not an expert in ruby by any means, so if I flub up my code or if there's a better way to do something, please leave a comment and illuminate me. So, here we go!

Read the rest of this entry

Simple Rails RJS Tutorial

May 13th, 2006

"Write javascript without writing javascript!" It sounds so zen... There is no spoon. Ahem. One thing I've read about since I've started fooling with Rails, and especially since it was officially included in 1.1 is .rjs templates and how they allow rails developers to totally skip writing javascript code. It sounded just like something I ought to learn about and then write a tutorial on...

Read the rest of this entry
Here's some video of the app that I wrote using my delicious script, an ajax progress indicator and scriptaculous effects. It's not very exciting, but it does show the behaviors in a more explicit way than just reading about it. I have another video I hope to post soon where I go over the actual code a bit.

Please leave me a comment and let me know what you think!



Test

Scriptaclous Cheatsheet

April 28th, 2006

I know that this has been all over the internet recently, and although I have it linked in my scriptaculous tutorial, I wanted to give it its own post.

Awesome Scriptaculous CheatSheet(PDF)
Thanks once again for inspiration to Brian Eng of Softies on Rails, here's my latest tutorial. Brian mentioned how easy it would be to add script.aculo.us effects to my little delicious app. And he was totally correct. What are script.aculo.us effects, you ask? Go check out this page. Click on each of the little demo boxes and watch the magic. Now say, "Holy crap! That's cool!" Then come back and I'll tell you how to add any of that to your Ruby on Rails app.

Read the rest of this entry
Ok, so my little del.icio.us app (click link to read about how I added Ajax functionality to a simple Rails app) is pretty cool, but it was missing one big thing. When the user clicks the "Get Results" link she has no idea that the page is communicating with the server. While the communication doesn't go on forever, an impatient user might click the link multiple times.

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:
This means we're waiting for a return back from the server: Spinner

Read the rest of this entry

RubyNoob gets all Ajaxy

April 23rd, 2006

Or:

"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
Long story short for those who aren't thrilled with my prose. If your Rails log file(s) are growing too large, log into your shell account and run this:

rake clear_logs
Now, back to the REST of the story:
Read the rest of this entry
One of the first projects that I have planned for Ruby on Rails involves calling the del.icio.us API and formatting results tailored to each user's desires. Actually, Lindsay thought the whole idea up, and it probably won't make much sense until we debut it "sometime soon."

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