Archive of posts with category 'Ruby on Rails'

Finally Done

It basically took all day today to sync the files from my laptop to my new coldandheartless server. It took me a bit longer than it should have to create the new linode because I was stupid. I forgot that I had used letsencrypt to get certificates for the files. And I forgot to open the https port and then spent way too much time wondering why the websites didn’t work.

Ruby Gem Error

I’ve been doing a bit more work with Sinatra and I got the following error. I’m just documenting how I fixed it.

Installing Rails 2.5.0 on Mac OS High Sierra

Since I’ve just switched to my new laptop running High Sierra, I thought it would be good to update my rails software. The latest version is 2.5.0. I downloaded it and compiled it and got an error message when I tried to install rails.

Uploading a File in a Rails Site to Use to Update

I want to upload a file to my rails webpage, but not store it in the database. I just want to upload it and then get the data out of it and update the database with the information in the uploaded file.

Paperclip with Spoof File Checks

I have a rails site that accepts pdf files as uploads. Based on previous experience, I have found that trying to validate on content-type causes lots of problems. There are many browsers that send weird content types with file names. For pdf files, they should always be using ‘application/pdf’ and most browsers do. However, this morning I got email from someone who couldn’t upload his file in one browser. He told me he used a different browser and it then worked. I searched the log files and found a bunch of error messages, but the main bit was this:

Declarative Authorization attribute rules

I have a website where I use declarative authorization to determine who is allowed to do what. One thing that I need to allow is for people to change their own passwords. It’s pretty simple, my model/controller is users. Here’s what I needed to do:

Making a PDF File and Automatically Attaching It

I’m writing a rails site that collects information from an applicant and references from other people that will then be reviewed by a different group of people. For the programmer, it’s easy to present the information from the database on a webpage and then provide links to uploaded documents. However, this is not necessarily the easiest way for the reviewers to read all the information. Based on other sites that do the same type of job, I’ve learned that reviewers want to be able to download a single file with all the information in it. I’ve put together various hacks in the past to do this, but have recently come up with a proper way. The following isn’t complete, but it presents the basics of how to do this.

Multiple Authentication Methods with Authlogic

I have a website where users need to login to be able to comment on pictures. For the most part, users will use their university credentials to login. However, I’ve found a few recently retired people who would be able to help us identify people in the pictures. But since they are retired, their university credentials are no longer valid. So for these people, I’d like to be able to make a local account for them to login. My rails app uses authlogic to check credentials. And I’ve been authenticating against the university ldap server for a while without any problems, but mixing in local accounts is going to require a few changes.

Crazy Error

I just updated some rails webpages to 4.2.4. On one system, I didn’t have any trouble. But on the other one, things worked fine on my laptop, but gave all sorts of random errors on my production system. I started keeping a list to see if I could figure out what was going on. Here are some of the errors that I was seeing.

Links vs Buttons

Now that I’m writing more websites, I’m trying to make them better. A lot of it has to do with making them look better so that people have a better (UX) user experience. I’ve been using bootstrap more, which is nice. But now I need to get into more nuts and bolts stuff.

Capistrano 3 Issues

I decided to give capistrano 3 a try with one of my projects. For the most part, I prefer version 2 to version 3. Mainly this is because I never have a staging server. I only have my development stuff and then I deploy to production. So having to specify the deployment server is just additional work that I always forget.

incompatible character encodings: UTF-8 and ASCII-8BIT) in Rails App

I got this error after I updated a website from rails3 to rails4. I spent a few days going over character sets and collations in MariaDB. It turns out that all I needed to do was to use the mysql2 gem instead of the mysql gem. I am writing this so I hopefully will remember when I do this again.

Authlogic Error

I put up a new website that wouldn’t allow me to login and kept giving me an error that said something like undefined method `save_without_session_maintenance’. I double-checked all my controllers and models against another site that was working fine and they all matched up. I didn’t find any bug reports for the version of Authlogic that I was using. The solution was to just deploy the app again. After that, I was able to login successfully. Weird.

Production Gems

I’m not sure how I didn’t notice this before, but my deployment on an app was suddenly taking much longer that it had in the past. While I was watching it run, I noticed that when bundle install was running, it was saying installing each gem. I don’t know why I didn’t notice this before. I looked on my server and noticed that each release was about 177mb in size and the vendor/bundle directory was 173mb in size. I was reading the Bundler website when I saw this bit:

Lots to Learn about Assets

I upgraded a new app to use Rails 4.2 and bootstrap and I thought things were fine. However, when I went to the page today, css wasn’t working. It just never created the css files. Turns out that I needed to add the ‘sprockets-rails’ gem to my Gemfile because the asset pipeline is no longer a feature of rails 4. Once I ran bundle and commit this change to the repo, I could deploy and things worked fine.

Still Learning About Rails and Deployment

I had been feeling pretty good about my Rails skills and had been able to create and deploy a number of simple applications. Since simple applications are just about all I’m going to write, I was ok with things. But I was hanging on to earlier versions of things and it was starting to bug me. One big one was the capistrano gem. I knew there were big changes between version 2 and 3, and I had a pretty good setup using version 2. However, I know that things constantly change so I decided to dive in and start using capistrano version 3, 3.4.0 to be exact.

Changing Rails Boolean with a Button

I suddenly realized how to change a boolean with a button in a much easier way that the post I did yesterday. All I did was make a form with one hidden field (the boolean that I want to change) and just show the submit button.

Change a Boolean in Rails with a Button

There’s a much easier way to do this, which I show in this post.

Rails Rewrite

I have a website that I’ve been using to track timesheets for a number of people. It’s one of the first websites I wrote in rails, written about four years ago. The only person who really uses it is me, but a few other people also login to view the information. Anyway, the site was written in rails 3.2.11 and now the latest version is 4.2. I’ve decided that since I know a bit more rails now (I wouldn’t say I’m a good programmer), I’d rewrite the program. It’s not that elaborate, but it does work really well for me.

Forms and Tables

I had a very weird situation today where a simple form I had created in rails would not work for the edit action. It would work ok for the new action. But if I tried to edit an entry, the submit button wouldn’t do anything.

Responsive Webpages

I’ve started using the twitter-bootstrap gem to get responsive webpages. It worked fine on my laptop and resized things correctly as I changed the size of the browser window. However, when I checked it on my phone, it was showing the full-size page. To fix this, I needed to add the following to the head in my layout.

Rails Program Upgrades

I had a computer that was hosting a number of simple rails programs that I needed to upgrade. So, I updated everything and reloaded the mysql data and everything was good. That is, until I tried to login. I had a number of unsuccessful attempts and just assumed it was because I forgot the password. There were actually three programs that I had to reinstall and after the third one, I realized that I hadn’t forgotten the password but that something had changed.

Ruby/Rails Upgrades

I have a few apps running on rails 3.2.12 and ruby 1.9.2. Since I need to make some changes to them, I thought I’d update them to rails 4.1 and ruby 2.1. I first upgraded the apps to rails 4.0 and ruby 2.0. That worked fine. Now I want to upgrade to ruby 2.1. First I’m doing it on my laptop. Here is how I compiled and installed it.

Rails Constantize Method

I learned a new inflector method the other day, constantize. Here’s the method I used it in.

Paperclip and Nested Attributes

I have an app that has a model (called cases) that has_many documents associated with it. I have had no problems in creating the case and the associated documents. I had quite a bit of trouble when I was trying to replace one of the documents with and updated one. But I finally got that straightened out. Not sure if this is how a real programmer would do it, but it works for me.

Hate Things that Depend on Whitespace

I’m writing a rails app and was trying to use a scope with a variable. My code looked like this:

Mass Mailing in Rails

I have a need to send out a bunch of individual emails from the same email address. I thought it would be good practice for me to see if I could do this as part of a rails app. The interesting thing is that I don’t want to save anything to a model. I just want to be able to upload a csv file, get all the names and email addresses out and send the same email with attachment to all of them. Here’s how I did it.

Rails4 and Assets

I spent a ridiculous amount of time trying to get a background image to show on a production server running a new rails4 app. Everything looked fine on my laptop, but once I deployed to the server, the image wouldn’t show. I only had one image on the site, but I’m pretty sure that if I had more, none of them would have showed.

Pittsburgh Trip

I just got back from a long weekend spent in Pittsburgh. I went there to attend Steel City Ruby Conf, which was pretty good. The first day had a lot of self-described “touchy-feely” talks, which I wasn’t crazy about, but understood why they were being given. Though Julie Pagano’s talk on Front-End Development was pretty good and Jim Weinrich’s “Friendly Flying Robots” was a lot of fun. The talks on Day 2 were much more what I was looking forward to. The talk on git by the Steven Ball was really informative and I’m hoping I can start using some of his tips right away. Avdi Grimm’s talk on Pair Programming has pretty much convinced me that I should give it a try as soon as I get some time. And Leon Gersing’s inspiring talk was the perfect finish. So, based on the strength of those talks, I feel it was a pretty good conference.

Fixing Inflections for Acronyms

In my app, there are a list of files that people need to upload. I had set things up so that they could pick the type of file from a dropdown list. The list of files are:

Rails Basics

These are just some notes on doing everything in rails by hand.

Rails Controllers

I’m starting a new rails app, using Rails4, and am trying to do more things by hand instead of depending on generators. I actually think generators are great, but I want to better understand how all the parts fit together. So I want to know how to do things by hand, instead of depending on the generators.

Requiring https

My apps tend to just be electronic sign-up sheets for various events. There’s no way we’re going to pay for an SSL certificate for a site that will probably be up for a month or two, at most. However, we do require people administering the page to login, which means passwords which need to be encrypted. That’s really all I care about encrypting, not the entire site. So, my somewhat simple solution is:

More Capistrano

Capistrano used to have a pair of tasks called deploy:web:disable and deploy:web:enable that I really liked. All they basically did was to upload a file to the webserver that would be displayed while a new version was deploying. I thought this was really helpful because if anyone visited the site during the deployment, they’d see the message that the site was under maintenance. Without this file, visitors to the site would see a cryptic rails rack error that made no sense. Sometime last year, after a capistrano update, I realized that these tasks were no longer there. By that time, I had understood that it was just uploading a file, so I could duplicate the tasks manually by editing a file to add the current time and then uploading it to the webserver. (In either case, I had to modify my httpd.conf file to reflect the location of the file.) Then, after the update, I’d remove the file. This wasn’t difficult, but it was WAY easier to do this:

Spoke Too Soon

When I came home from work, I was feeling a little full of myself and then I wrote my previous blog post. I spent the past couple of hours watching some Railscasts that were really informative, on using capistrano to do all sorts of things. So I changed my capistrano file quite a bit and do feel that I understand it a bit more.

Advancing

Today, I started a new ruby on rails website for a workshop. It’s a very basic site that just handles registrations and redirects people to the credit card processor that we use. This is the main type of website that I write in rails and it’s probably my tenth site that I’ve written for this purpose. Previously, I had always used scaffolding in rails and then deleted the stuff that I didn’t need. This time, I made a conscious choice to not use any scaffolding and instead generate my own controllers and models. I’m happy to say that I didn’t have any real problems. So I’m thinking that I’m no longer a complete beginner with rails. I might even go so far as to say I’m an intermediate rails programmer. Which means it’s time for me to start advancing some more. I found this website http://learn.thoughtbot.com and was thinking of signing up for the prime program. The intermediate ruby on rails workshop looks pretty interesting. Though, after reading the syllabus, I’m thinking that perhaps I was at intermediate level a while ago. In the listing of things that they plan to cover, I already know a few:

More Ruby Notes

I just found out that these two ruby commands do the same thing.

Rails3 form_for vs form_tag

I’ve been advancing a little in my rails work, to the point where I’m making more elaborate forms. So I’ve had to switch to using form_tag instead of the form_for that’s the default when you use scaffolding. I have a pretty good understanding of the differences now, so I’m documenting it here.

Drop a Column in MySQL Table

For some reason, my migration file wouldn’t drop a column when I tried to go back to an earlier version. I fixed this by just going into mysql and dropping the table manually.

Simple Dropdown Menu of Numbers

Every so often I need to make a simple dropdown menu of numbers in my app. And for some strange reason, I always seem to forget how to do this. So here are some examples for myself to use when I forget the next time.

Update Multiple Boolean Attributes in Rails3

We are hosting an event where students need to submit two pieces of paper and pay to attend. The model I’ve created has three boolean values for these three items: safety_waiver, image_waiver and paid. As the forms and payments come in, I could manually edit each entry and check off who has paid or not. But, since I’ve done a few of these events in the past, I know it would be easier if I could just see a list of everyone who hasn’t paid or turned in their waivers and check a bunch at once. I tried following Railscast #52, which was great and almost got me there, but didn’t work for me in the end. So here’s what I did.

Rails and Autocomplete

I recently wrote a simple rails app to let people record measurements (current and voltage) on for a given plate. Each plate can have many measurements and each measurement belongs to only one plate. I wanted to make it easy for people to enter the data and there might be a fair number of plates, so I didn’t want people to have to scroll down a long drop down menu to pick a plate. This seemed like an ideal situation to use autocomplete on. Basically, the home screen is just the new measurements path. So, putting in the measurements will be easy. I then wanted to have a text field for the plate name that has autocomplete enabled. As the user enters in information for each plate, a little drop down list will popup that users can use to pick the correct plate. And if it’s a new plate, it will automatically add it to the database.

Coffeescript is Whitespace Sensitive

I’m attempting to get autocomplete working on a rails app. I’m basically following the instructions in Railscast #102 (revised). It wasn’t working because I didn’t realize that coffeescript was whitespace sensitive. Once I realized that, I thought I was set, but I wasn’t. I like having my tabs be two spaces, but coffeescript wants four space tabs. So after I doubled all of my tabs, things started working.

Ruby and Ldap

I’ve been rewriting a script I use that lets people update their linux and samba user passwords at the same time. Our server uses samba and openldap to behave as a primary domain controller for our windows computers and a single sign-on for a few other services. It’s been working great for years and way back when we started using this, I wrote a perl script to take care of keeping the two passwords in sync. The original problem was that if you use the regular linux passwd command, the linux password will change, but the samba one will not. The samba password uses an nthash. In a given user’s ldap account, there is the userPassword field which holds the linux password and the sambaNTPassword field which holds the samba password. For a user to be able to use any of the web services we run on the server and login to any of our windows computers with the same password, these two fields need to contain the same password.

Don’t Know Why This Didn’t Work Yesterday
[~]# gem install rails -v "3.1.3"
Ransack with Nested Attributes

In my app, I track plates which have a number of properties. They also have a location because I need to track where each plate has been. However, for the most part, what I really care about is where the plate is now, its current location. When I do my ransack search on the plates, I want to be able to have some check boxes with the possible locations. So, for example, people could search for all the plates that were annealed at a certain temperature and are located at Chicago. This means I have to search the locations for a given plate, but only for the latest location. I tried a number of different ways to do this in ransack and didn’t have much luck. I got it working how I wanted, but I’m fairly certain that this isn’t the best way to do it.

Ransack Update

I have an earlier post describing the way I set up a ransack search that sets up check boxes for possible search values in a given field. This was my first time using ransack and to say I had no idea what I was doing was not too far off. I’ve recently had to use it for another app and took some time looking into it again. And I think I’ve found the correct way to do a search like this.

Rails Forms with Check Boxes

In my latest rails app, I want to compare a number of objects. My first solution was to create a new search page with a form on it.

Ransack Integration Test

In my efforts to test the right stuff, I wanted to test that my basic search was working properly. I have a collection of plates that we’re storing a bunch of information on. Each plate has a unique serial number. We also have a unique id for each plate, but for our users, the serial number is what they’ll use to identify it. So I wanted to make a form that would let people search for a particular plate based on the serial number. And since this search form will be on my home controller and not the plate controller, I need to use an integration test to do this.

Dropdown Lists

In the event that I want to make a simple dropdown list in a form in rails, I can use this:

Ransack Gem

Recently, I started using the ransack gem (https://github.com/ernie/ransack) for searching in one of my apps. I’m putting this here as an example, in case I ever need to make any more changes. The basics of ransack are pretty easy to do and work decently well. First step, add a search method to your controller. It should look something like this:

Routes

I’m working on a new app for work. Basically, it’s just another app that lets people put up some info and an image to go with it. The difference to this one is that I’m storing information on glass plates, which each have a unique identifier that’s not an integer. I didn’t want to mess around with changing the primary key from id to something else, because later I’m going to be using it with some other tables. Instead, what I’d like to do is be able to show the image associated with the plate at a url that uses the unique identifier. So, instead of something like plates/12, I could download the info at sample/538-12122. Here’s what I did.

Changing link_to to button_to

In one of my rails apps, I send out email to a particular user. But, I can look at the message before it goes out. Then, at the bottom of this preview page is a link to actually send the message. I’m pretty sure that since this action is changing a boolean value on the user along with sending the email, that I should be using a button instead of a link. However, just switching the link_to to button_to won’t work because link_to uses a GET request, but button_to uses a POST request. So I need to change the button_to to use GET. The way to do that is like this:

Detaching Rails Processes

In my latest app, I’m emailing reports to people who click on a link. (Yes, I know this should be a button, but I just did it with a link because it was easy. If I have time, I’ll change it to a button.) Anyway, I’m attempting to use Process.fork to do this. What I have that works is:

Rails Integration Tests

After unit tests, for me, the important tests are integration tests. This is where I can test logging in and checking authorizations and calculations. They are a bit more involved, but not too difficult once you get the hang of them.

Rails Unit/Functional Tests

In my latest rails project, I am trying to write good tests for everything important. My first tests were all unit tests. These are pretty easy. All they basically do is make sure that the data that’s stored in the database is formatted properly. So I have a bunch of tests that make sure that my validations are correct. A sample test looks like this:

Toggling a Div On/Off and Setting Default State

In order to have a section of a webpage toggle on and off, I need to use the toggle function that is provided by jquery.

Nested Attributes, Forms and Validations

I have a simple app where people can apply for a position. The table for this is applicants. Then, all users are allowed to rate each applicant. The ratings are in a table called ratings, which just has an applicant_id, user_id and score. So our tables are:

Moving to Ruby 1.9.3 and Rails 3.1.1

I’ve used rvm to install ruby 1.9.3-p0. Unfortunately, after installing, I get these errors:

My New Rails Deploy Method

I recently learned that capistrano can temporarily take down the web server while it’s being updated. This sounded like a very good idea to me, so I’ve started to use it. The only change I needed to make was to my /etc/httpd/conf/httpd.conf file. My virtual host now looks like this:

Validating Nested Attributes

I am sure that there’s a better way to do this, but I got this working and I wanted to document it.

Multiple Rails Versions

If I have a number of different versions of rails, I can create projects for a particular version with:

rvm notes
rvm list
Rails3 and Standard Library CSV

Recently I had learned how to write a csv file in my rails app using the fasterCSV gem. In Rails3, CSV is now part of the standard library, so things are a little different. I also did a few trickier things this time around, so I’m updating my notes.

Rails 3 Table with Fields Using Calculated Data

I have a table in my app, called entries. Entries belongs_to :users and has a field (decimal) called hours. There is another decimal field called cost. Cost is not entered by the user, but is calculated by multiplying the value in hours with the value in users named rate. To get this to work properly, I had to do the following.

Downloading Spreadsheet from Rails

I have a rails app that needs to provide a spreadsheet of all the data in one of the tables. Specifically, we have a simple signup form for an event. I need to be able to let a subset of users download a spreadsheet of the current people signed up with the information given on the signup form. Here’s what I did.

Rails3, ldap and authlogic

I have an openldap server running and wanted to use it for authentication on a rails 3 application.

Rails3 and Gemfile

I’ve had some problems with the new bundle install in rails 3. Specifically, on my laptop, in the development environment, I use sqlite. But in production, I use mysql. I was getting all sorts of errors on my laptop about the mysql gem not working and on the production server the errors were about a problem with the sqlite gem. Ideally, I wanted the development server to ignore mysql and the production server to ignore sqlite. Here’s how it was done.

Rails and params hash (from Google Cache)

Originally posted January 7, 2010