Redirect to log in page if user is not authenticated with Devise

Just simple add this method to application_controller.rb protected def authenticate_user! if user_signed_in? super else redirect_to login_path, :notice => ‘if you want to add a notice’ ## if you want render 404 page ## render :file => File.join(Rails.root, ‘public/404’), :formats => [:html], :status => 404, :layout => false end end And you can call this method … Read more

Determine if a string is a valid float value

Here’s one way: class String def valid_float? # The double negation turns this into an actual boolean true – if you’re # okay with “truthy” values (like 0.0), you can remove it. !!Float(self) rescue false end end “a”.valid_float? #false “2.4”.valid_float? #true If you want to avoid the monkey-patch of String, you could always make this … Read more

heroku run console returns ‘Error connecting to process’

I had the same problem, and although I did not solve the problem, I found a workaround. Instead of using: heroku run rake db:migrate You can use: heroku run:detached rake db:migrate This runs the command in the background, writing the output to the log. When it is finished you can view the log for the … Read more

rails assets pipeline “Cannot allocate memory – nodejs”

It’s simple to spend the three minutes (maybe two if you type fast) to add a swap file to your server. If you’re running Ubuntu (not sure how well this works for other Linux flavors), just follow this tutorial from DigitalOcean: https://www.digitalocean.com/community/articles/how-to-add-swap-on-ubuntu-12-04 Voila!