I’m in and out of servers all day. One thing I’ve found helpful is a ssh config file. Create a file and place it ~/.ssh/config
. You can add as many entries as you like.
Host s3-rsync
HostName ec2-54-203-43-190.us-west-2.compute.amazonaws.com
User ec2-user
IdentityFile "~/.ssh/private-key-location"
Host: A friendly name/handle which you’ll use.
HostName: The remote server address you’ll be connecting to.
User: The user you want to connect as.
IdentityFile: The private key to authenticate with.
Once its all setup you can connect by typing: ssh s3-rsync
This is the same as: ssh -i ~/.ssh/bs2-deploy ec2-user@ec2-54-203-43-190.us-west-2.compute.amazonaws.com
.
Rails has taught me some really good best practices. Let’s look at a default rails create action
def create
@post = Post.new(post_params)
respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was successfully created.' }
else
format.html { render :new }
end
end
end
I try not to deviate too much from this and nor should you. If @post.save
returns true. return a 201 status code. If @post.save
returns false re-render the :new action. So you can let the user fix the form.
Often I will see things like this.
def create
if params[:something]
@post = Post.create(post_params)
else
@post = Post.create(other_params)
end
redirect_to posts_path
end
At first glance this looks clean. But if you look closer. There is a subtle problem here. If the post object fails validation and fails to create, The action redirects to the index action where all new instaces are creating. The @post object has lost any knowlege of the params and it will not get to oportunity to display the errors from the object. You’ll have to resort to things like flash[:notice] = "@post.errors.full_messages.join()"
… ugh.
Active record gives us a nice interface for displaying errors on an object.
class Post < ActiveRecord::Base
validates :title, presence: true
end
irb(main):001:0> p = Post.new
=>
irb(main):002:0> p.valid?
=> false
irb(main):003:0> p.errors
=>
@messages={:title=>["can't be blank"]}>
irb(main):004:0>
When you call @post.save
OR @post.valid?
errors are added to the @post object so they can be displayed within the form.
Digging even deeper… Check out ActiveModel::Model. It will give you this same object interface without needing database backed model. Powerful stuff.
Lately I’ve been writing a lot of angularjs. At my work, we had a great debeate.. which client side framework should we use. It came down to angularjs or ember. Ultimately, we choose angular. I’m happy we did. Angularjs is awesome. I would have been just as happy to settle with ember. Two way binding is a game changer.
Rails is great, BUT, I feel that, in any rails app, you end up creating interfaces that are a reflection of your database. Adding a client side framework, you’re free to create interfaces that are decoupled from your backend.
Lately I’ve been doing a lot of server administration. It can be a lot of fun automating systems. One thing I’ve found super valuable is to setup a “message of the day” file. This will display immediately after login and can be found in /etc/motd
. I like to put notes/documentation here. For example, I’ll put in how to restart redis, how to tag a deploy, or the path to the app.
I love the recent shift from dynamic sites to static sites. This doesn’t work for all sites. But why have a blog that gets dynamically build on each request. It’s Not needed.
Amazon will host static sites on an S3 bucket and its dead simple. Awesome!
- Create a bucket and set as public host.
- Upload files and make them public.
- Profit.