To run a rake task within a shell script using a cron job, do the following: First, specify the shell at the top of your .sh script #!/bin/sh Then, change your directory to your rails application in your .sh script cd /home/user/app/current Now, you need to manually define your PATH within the script: […]
Category: Rails
Rails Admin: Conditionally display a field based on a specific object’s value
To conditionally display a field based on a value in the displayed object, you can do the following: RailsAdmin.config do |config| config.model YourObject do edit do field :some_random_attribute do visible do bindings[:object].your_conditional_field==”your_conditional_value” end end end end end In this Rails Admin configuration example, you can see that the YourObject#some_random_attribute field will only display if […]
Rails Admin Custom Dashboard
To create a custom rails_admin dashboard, first create a new file here: lib/rails_admin.rb And in your rails_config/initializers/rails_admin.rb initializer file, be sure to include the above file: RailsAdmin.config do |config| require Rails.root.join(‘lib’, ‘rails_admin.rb’) end Within this new file, do the following: module RailsAdmin module Config module Actions class Dashboard < RailsAdmin::Config::Actions::Base RailsAdmin::Config::Actions.register(self) register_instance_option :root? […]
Install RMagick on Ubuntu 14.04
If you’re running into the following error when attempting to install the RMagick gem: ERROR: Error installing rmagick: ERROR: Failed to build gem native extension. Be sure to install the dependencies, then try again. sudo apt-get install imagemagick sudo apt-get install libmagickwand-dev After which you should be good to go.
Rails conditional migrations – ensure column does not exist
If you’re running into a rails migration exception, such as: column “x” of relation “y” already exists Then you can conditionally run the associated migration in the following way: YourModel.reset_column_information unless YourModel.column_names.include?(“the_column”) add_column(:your_models, :the_column, :boolean, :default=>true) end Of course, it is probably best to address the underlying workflow issues that caused this issue to […]
Rails 4, Carrierwave, Remote file url uploading
If you’re running into the following error when attempting to use Carrierwave to upload a remote file url: TypeError: no implicit conversion of nil into String Then you’re probably very frustrated at the moment. I dug into the carrierwave gem to figure the issue out exactly, however I realized there was an easier (and much […]
Sunspot Solr – Indexing PDF documents
Indexing PDF documents using sunspot / solr seemed like a difficult task initially. There are a number of tutorials dealing with this topic but they seemed overly complicated. In the end, I decided the simplest way to go about providing search functionality for indexed PDF documents was to 1) parse the documents locally 2) input […]
Rails override default scope ordering
If you have an ordering default scope that you’d like to override with a different ordering, you can do the following: Change the order() method to .reorder() This will remove all pre-existing default scope orderings and replace them with the argument found within the reorder request.
Heroku Rails 4 assets missing
If you’ve recently loaded a new Rails 4 application to Heroku and your assets are missing (css, js, etc) – you’re not alone. Add the following lines to your production.rb (or staging.rb) environment configuration files: config.serve_static_assets = true config.assets.compile = true Then push the changes up to Heroku and you’re good to go.
Rails remote multipart form with remotipart bind ajax:success
Rails 3.x / 4.0 does not handle remote multipart forms natively, so a workaround is needed. To get remote multipart forms working correctly you can use the following steps (quick solution). First: Add the remotipart gem to your GemFile gem ‘remotipart’, ‘~> 1.2.1’ Then, Update your javascript manifest file (in basic configurations this is usually […]