Monthly Archives: December 2015

Fixing Rails blank select value insertion

Have spent some time getting back into the HABTM (has_and_belongs_to_many) fun and games with Rails, using association tables to model a many-many relation and a multiple select form element.

But when selecting an item from the list and submitting the form I get the following (truly appalling) error:

undefined method `name' for nil:NilClass
self.version_ids = ids

The highlighted line is the one hving the problem.

Nowhere on any of the code being worked on is there any reference to the ‘name’ and why is there a nil class?

A clue comes from looking at the submitted values which includes the following:

"release"=>{"name"=>"release_name", "version_ids"=>["", "1"], ...

There’s a blank value that is not part of the select form that is being inserted by Rails (for good reason) which accounts for the nil class (but not the name reference).

http://stackoverflow.com/questions/8929230/why-is-the-first-element-always-blank-in-my-rails-multi-select-using-an-embedde discusses this and suggests some workarounds, but it could one of the things that people end up hating Rails for.

But, even after adding  a suggested fix in the controller,

params[:release][:version_ids].delete("")

Which removes the blank value; I still get the same error. This is looking like one of those Rails code typo bugs where I’m missing a capitalization or plural somewhere that will take a day to spot because of the ridiculously misleading error message: there is no nil class being passed and there’s no request for the name method of any passed object.

UPDATE: I checked another part of my application that I know to be working and uses the same method: same error. A bit more research turns up a link which suggests that downgrading to Ruby 2.1.5 (from 2.2.0) fixes the problem (with Rails 4.1.0). And this page says that upgrading to Rails 4.4.11 (to work with Ruby 2.2.4) will resolve. I have done both. Like, I said, I knew this wasn’t going to be a problem with my code, and this is exactly why people hate Ruby/Rails; I got a knowing look from a Pythonista I told this to.

Advertisements

Testing Rails application with authentication

The following link might help with doing some help when testing applications that require authentication to perform certain actions.

https://github.com/plataformatec/devise/wiki/How-To:-Test-controllers-with-Rails-3-and-4-(and-RSpec).

Update: But you know what? Rather than trying to spoof an LDAP login session, it’s easier to install OpenLDAP and create a simple Person record and auth against that. It does mean that I won’t be doing any of this development on Windows, as if that was ever likely to cross my mind.

Custom Rails generator – note to self

A quick reminder on how to use a custom generator to create a controller

$ rails g applist_controller NameOfModel

Which will create app/controllers/name_of_models_controller.rb.

But remembering that this will probably prompt to overwrite any controller that was created using a scaffold command. The generator will be found in the file, lib/generators/rails/applist_controller/applist_controller_generator.rb; they’re quite fiddly to write.

The scaffold command can create model and view files (using the contents of lib/templates/erb/scaffold. Using a custom scaffold controller (in lib/rails/scaffold_controller/controller.rb) doesn’t work. Maybe there are other ways to use this with the scaffold (c.f., http://guides.rubyonrails.org/generators.html).

Update: A helpful page has suggested that the controller template is saved in lib/generators/rails/scaffold_controller/scaffold_controller_generator.rb. (Obvious when you think about it, really); they’re no easier to write, though.

A few more tweaks are needed once installed but it does 90% of the work with 10% of the effort so well worth the time spent getting it right.

PhusionPassenger on Arch borked

Despite my optimism regarding getting my Rails app running under Apache httpd with Passenger, I wasn’t counting on Arch Linux getting in the way.

After non-sensical permission denied errors in the Apache logs, with Passenger running, but nothing in the Rails app logs, I figured that the problem was that Passenger was actually starting the app.

Running Passenger standalone seemed to suggest it wanted to install and run nginx trying to serve content from an Apache httpd confguration directory (which doesn’t exist). The lack of any errors with regard to Rails not actually starting doesn’t help.

It could be that the Ruby/gems/Rails install is pretty messed up (hard to imagine, I know) but it looks like I’m back to laptop Fedora Apache after another day lost faffing around with the web server side rather than working on code; I’m not working on a Rails app that has sensitive config values in code files that can be checked into GitHub and Apache httpd is the only way to go.

PhusionPassenger with Nginx for Rails – nearly worked

Now, I have a great deal of time for the PhusionPassenger module used to proxy Rails (and other framework) applications into a web server and I suspect the problem is that I’m doing something wrong; this is the first time I have tried it on Nginx.

I followed the guides at https://www.phusionpassenger.com/library/deploy/nginx/deploy/ruby/ and https://wiki.archlinux.org/index.php/Ruby_on_Rails but after configuring everything for my Rails app, I just get the ‘Welcome’ page. The Passenger processes are running although the logs seem to suggest otherwise.

There’s also a problem with scaling Nginx: with Apache we can define sensitive application parameters (such as database password and secret key) as environment variables for each vhost. Nginx doesn’t support this way of working which has to make code deployment harder for a single site let alone multiple vhosts.

I know the app is working because it starts from the console with ‘rails s’.

I’m probably on firmer ground with Apache providing Rails apps so I will give that a go instead; Passenger is a joy to install this way. While Nginx is hip and funky, I think I need the maturity and stability that you can only get with Apache httpd.

Arch Linu MySQL (Mariadb) startup

Posted as a quick note-to-self following a delay getting a database up and running for a rehash f an old Rails project.

After installing the mariadb package on Arch, the database needs to be initialised before starting,

# mysql_install_db --user=mysql --basedir=/usr --datadir=/var/lib/mysql

After this, the database server can be started with,

# systemctl start mysqld.service

(Still can’t say that I’m happy with systemd; I just can’t see what problem it’s trying to solve).

https://wiki.archlinux.org/index.php/MySQL provided the much-appreaciated help and guidance in this case.

Killing the horrors of vim

I know most youngsters love all the cleverness of vim but sometimes it’s a bit too clever for its own good and I have been having a nightmare trying to copy and paste some XML into a file on a Linux server; vim refuses to recognise the noai option to turn off auto indentation and makesva complete mess of the edit.
There is, however, an option to rescue the situation,
:set cp
Or
:set compatible

Which turns off all (well most) of the advanced features that make vim so hard to use on a server.
My XML pastes first time although syntax highlighting is still on, but it is such a big improvement.