Over the holidays I spent some time with my parents up in Grand Junction, CO, where the only cable provider is Bresnan Communications. I had never used Bresnan before, and my parents had just got service with them a month before this. So one morning I get up to get some work done, read up on some news and see whats new in the twitterverse. As I'm reading I find a few different interesting links I want to read, I open those links up and am greeted not with the website I wanted, but instead with this site.
Instead of taking me to the requested website, Bresnan redirected me back to their search results for 'bit.ly' (Which is the TLD of the site I was going to, the full link was http://bit.ly/sMeo). Notice that the first 5 links are SPONSORED links. This means Bresnan gets money when you click on one of their sponsored ads. I would also like to note that 5 of first the 6 results on the page are sponsored. At first I didn't really think too much of this, it was the first time it happened. Maybe their DNS servers failed.
As I'm working a little later I pull up another site, and instead of the site I wanted I am again greeted with Bresnan's sponsored search results for the TLD of the website I wanted to be at in the first place.
Now they're starting to get my attention, so I go check out another stats monitoring website I use for a few of my personal sites, and again instead of the site, I am taken to Bresnans sponsored results for the site I requested
Now this is getting a little annoying, but I am eventually able to get to the sites that I had originally requested. Doing a Google Search for the website I wanted to visit usually worked to get around the DNS hijack.
What I started seeing next was more than a little annoying, it is illegal. As I was browsing some big websites (amazon.com, reddit.com, zenhabits.net and tvguide.com) I noticed that the ads put there by the website owners to make money were being replaced by Bresnan with Bresnan's own ads. So now even if a Bresnan customer was to click on an ad for say zenhabits, zenhanits would NOT get the money for that users click, since Bresnan replaced that ad with their own, and the customer is (usually) none-the-wiser.
Just visit any of these sites above to see what the ads on the website are suppose to be, then compare with that ads show up for Bresnan customers. These are clearly deceptive and illegal practices Bresnan is conducting. As an ISP you cannot hijack content from a website, and altering the content your customers see on a copywritten website is copyright infringment by Bresnan. I'm sure amazon.com, zenhabits.net and the rest would be very upset to know that their websites copywrights are being infringed upon, and they are also losing advertising money becuase Bresnan is trying to make a quick buck from their customers.
SWFUpload is a great javascript kit for adding nice, multiple file uploads to your application. Today were going to go through the steps to get SWFUpload working with Merb 1.x. All this code is valid for SWFUpload v 2.2.0, which you can download here.
Generate your new merb app
merb-gen app picture_place
Create the picture resource
We will need a picture model to store our pictures, so lets create that now.
merb-gen resource picture
Install dm-paperclip
Paperclip is a great gem for handling picture uploads. Open up /config/dependencies.rb and add the following
dependency "dm-paperclip"
Setup the picture model
Add the following to /app/models/picture.rb
include Paperclip::Resource
has_attached_file :image,
:default_url => "/uploads/pictures/:attachment/missing_:style.png",
:url => "/uploads/pictures/:id/:style/:basename.:extension",
:path => ":merb_root/public/uploads/pictures/:id/:style/:basename.:extension",
:styles => {:small => "55x55#", :medium => "100x100#", :large => "600x600>" }
The has_attached_file method is where you setup paperclip options. :default_url is the image to show if paperclip can't find the requested image. :url is the place where your images get stored. :path is the path the image is served from. :styles creates thumbnails out of your picture, you can define custom sizes here. The has_attached_file method will also add all the columns to the database that are needed such as content-type, filename etc.
Setup the router
Were going to want to use an action in the picture controller called 'swf_upload' to handle uploads coming from SWFUpload, so lets set that up in the router.
with(:controller => "pictures") do
match('/pictures/swf_upload').to(:action => "swf_upload").fixatable
end
Notice the .fixatable on the end of the match method, this is important for passing session info through flash and back again.
Setup the controller
Open up your pictures controller and add the following.
def swf_upload
only_provides :html
@picture = Picture.new
@picture.image = params[:Filedata]
if @picture.save
return "success"
else
return ""
end
end
This is the basic method for handling your uploads. @picture.image = params[:Filedata] is what sets up all the info about the picture for the database.
Get the necessary files
We are going to need a few javascript files to SWFUpload, so were gonna grab those now. First create a new directory in public/ called swfupload.
Then head over here, download all the files and put them in public/swfupload/
Now onto the view
Now for the fun stuff. Open up /app/views/pictures/new.html.haml and add the following
%script{:type => "text/javascript", :src => "/swfupload/swfupload.js"}
%script{:type => "text/javascript", :src => "/swfupload/swfupload.queue.js"}
%script{:type => "text/javascript", :src => "/swfupload/fileprogress.js"}
%script{:type => "text/javascript", :src => "/swfupload/handlers.js"}
:javascript
var swfu;
$(document).ready(function(){
var settings = {
upload_url: "/pictures/swf_upload?_picture_place_session_id=
flash_url: "/swfupload/swfupload.swf",
file_size_limit: "10485760", // 10 MB
file_types: "*.jpg;*.png;*.gif",
file_types_description: "JPG, GIF, or PNG Image Files",
file_upload_limit : 100,
file_queue_limit : 0,
debug: false,
custom_settings : {
progressTarget : "images"
},
// Button settings
button_width: "200",
button_height: "32",
button_placeholder_id: "browse",
button_text: '<span class="theFont">Select files to upload</span>',
button_text_style: ".theFont { font-size: 16; font-family: arial; }",
// Handlers
file_queued_handler : fileQueued,
file_dialog_complete_handler: fileDialogComplete,
upload_start_handler: uploadStart,
upload_progress_handler: uploadProgress,
upload_error_handler: uploadError,
upload_success_handler: uploadSuccess,
upload_complete_handler: uploadComplete
};
swfu = new SWFUpload(settings);
// Add Event Handlers
$('#browse').click(function() { swfu.selectFiles(); });
$('#upload').click(function() { swfu.startUpload(); });
});
%h1 Upload some new pictures
%a.upload-button
So whats going on here? The first two lines include the javascript files we need. Most of the settings are self explanatory, but note the upload_url line, were passing the users session key through so that we know which user uploaded the picture in the swf_upload action. This will give us a basic working upload form, go ahead and fire up merb and give it a shot.
Give it a test run
We now have working uploads, fire up your browser and head to http://localhost:4000/pictures/new and try uploading some pictures. I have a sample app made up on github you can check also look at.
MerbCamp wrapped up last weekend, and we were given Merb 1.0 RC1 . So I dove right in and upgraded a couple apps
from 0.9.8, and everything was fine until I deployed the apps in production under Passenger . The first thing is merb now comes with a public/.htaccess
file, and there are a couple lines we need to comment out in here so that Passenger doesn't use fcgi. Make sure the following lines are commented out in your public/.htaccess file
There is no need for fcgi behind Passenger since Passenger uses rack. Now you should be able to deploy your app behind Passenger without many problems. There is still one
problem you may come across. For some reason you can't have local variables set in a controller or your app will fail with the following error message
/usr/local/lib/rubyEE/lib/ruby/gems/1.8/gems/ParseTree-2.2.0/lib/parse_tree.rb:151:
[BUG] Segmentation fault
So instead of this
def show
@post = Post.get(params[:id])
number = 100
number = number + 100
end
You would need to do this
def show
@post = Post.get(params[:id])
@number = 100
@number = @number + 100
end
I have not yet figured out why local variables cause the segmentation fault, but this is a quick way to get around it so that you app will still work.
Over at the
Canadian Developer Connection they have a great list of 'you might be a web designer if' posted up. Just a couple of my favorites...
1. If you overuse AJAX for household chores, you might be a Web developer.
2. If you think "passing the Acid test" doesn't refer to a drug problem, you might be a Web developer.
3. If your "Yo Mamma" jokes include the phrase, "padding: 200%", you might be a Web developer.
Do you have some of your own that aren't on the list? Lets hear them in the comments.