Redirect page using JavaScript while maintaining page history

In one of my Rails views, I’m using a remote form that submits via AJAX. On the ajax:success callback, I wanted the page to be redirected, while allowing the user to go back i.e. maintain page history.

Here are two ideas that didn’t work:

  1. A ‘redirect_to’ call in the controller that handles the POST request. The redirect_to call is ignored.
  2. A ‘window.location.assign()’/’window.location.replace()’ JavaScript call in the ajax:success callback function. In this case, the page loads the new url but also replaces the original page in the browser history, so that the back button doesn’t work.

What worked was a hidden button which was linked to the redirect URL and JavaScript to trigger a click on that button, in the ajax:success callback.

[gist 4726028]

I also tried this out with an HTML link. However for some reason, the jQuery click() function does not trigger clicks on HTML links.

UPDATE: Turns out the back button works only the first time. If I do this twice, and then hit ‘Back’, I go back to the first page. [TODO: Elaborate.]

Multiple forms with different HTML DOM ids for objects of the same class in Rails

To be frank, I’ve never been very comfortable with Rails form helpers. Today, I had to display forms for multiple objects of the same class in the same view.

I went ahead and rendered the ‘form’ partial created by the scaffold for each of the objects. The form_for method adds ids to the form and they are the same for the same class. This leads to HTML validation issues and general discomfort if you are planning to use any Javascript on that view.

It took me longer than usual to find a solution for this. You have to use the namespace option in the form_for method. Read more here – http://apidock.com/rails/v3.2.8/ActionView/Helpers/FormHelper/form_for

Example follows in the gist below:

Enter password only once while logging into remote clusters like Myth / Corn

Most courses at Stanford require use of myth/corn clusters for the programming assignments.
I’ve tried using tmux on those clusters, but the experience hasn’t been very good. For some reason, when I detach the tmux session, log out, log in and attach the session again, it somehow loses permissions to write to my files.
Myth and Corn don’t work with ssh keys either, which means I have to resort to opening multiple terminal window/tabs and enter my password everytime.

Adding the following to my /etc/ssh_config file reduces some of the pain, by auto-logging me in, if I have one open session.

Credit goes to Kyle, for this handy tip.

Pry: a brilliant irb replacement

I’ve come across a few ways to improve my irb/rails console experience. You could either try to enhance it using hirb or wirble, or you could swap it out for the  powerful pry.

Here’s a basic comparative screenshot, to make you want to give it a shot. An example of the awesomeness that lies ahead is the ‘nesting’ feature, which allows you to dig into your classes and their instances the way you would dig into a folder tree. As always, the railscast on pry is pretty neat.

lol-worth: ‘wtf?’ prints out information about the last observed exception

Rails quicky: Reload console without exiting

I use the rails console a lot while fiddling around with pieces of my Rails code base. Every time I made changes, I’d exit it and restart it again. Until today. In a fortuitous sequence of events, I chanced upon the reload! command, which pulls in the latest changes to your code without needing to exit the console!

Continue reading

Booting up tmux sessions with customized windows and panes

I have been exploring easy ways of booting a custom tmux session. I gave the tmuxinator a shot today and really liked it.

I wish there was an easy way of shutting down my tmux session though without manually having to CTRL-D every pane.

This is the yml I’m using for my Rails projects:

This is what the tmux man page says about layouts. You can read more about layouts here.

M-1 to M-5  Arrange panes in one of the five preset layouts: even-
                       horizontal, even-vertical, main-horizontal, main-
                       vertical, or tiled.