Validating Boolean Value of False in Rails

I came across a bit of a weird one this afternoon when trying to validate. Several of my fields appeared to be incorrectly flagged as not having a value.

This is the validation I was using:

#Define validation
validates :order_id, :user_id, :acknowledged, :completed, :presence => true

 

The fields that were flagging as not present were :acknowledged and completed. After hardcoding the values and still not getting anywhere I realised that while values were being provided, they were boolean values of false. Apparently :presence will treat a value of false as not being present.

After another Google search I came across this post which shows how to validate a boolean value.

When implemented within my app, the new validation looks like this:

#Define validation
validates :order_id, :user_id, :presence => true
validates :acknowledged, :completed, :inclusion => {:in => [true, false]}

Hopefully that’ll be able to help a few of you out as well.

link.smartscreen.live.com – Google Analytics Referral

Well, today I was looking through the Google Analytics overview of the WhatIBroke.com and I noticed a referral I hadn’t seen before from link.smartscreen.live.com.

I did a bit of Googling and it turns out that this is actually something Microsoft uses within IE and MSN. Apparently, whenever someone clicks on a link in msn the service will check the link against a list of unsafe sites and warn the user if anything is amiss.

It just so happens that I’d sent a link to the blog to a friend on msn – so I guess everything checks out. Here’s a bit of a blurb from the FAQ on Microsoft’s Site:

SmartScreen Filter is a feature in Internet Explorer that helps detect phishing websites. SmartScreen Filter can also help protect you from downloading or installing malware (malicious software).

SmartScreen Filter helps to protect you in three ways:

As you browse the web, it analyses webpages and determines if they have any characteristics that might be suspicious. If it finds suspicious webpages, SmartScreen will display a message giving you an opportunity to provide feedback and advising you to proceed with caution.

SmartScreen Filter checks the sites you visit against a dynamic list of reported phishing sites and malicious software sites. If it finds a match, SmartScreen Filter will show you a warning notifying you that the site has been blocked for your safety.

SmartScreen Filter checks files that you download from the web against a list of reported malicious software sites and programs known to be unsafe. If it finds a match, SmartScreen Filter will warn you that the download has been blocked for your safety. SmartScreen Filter also checks the files that you download against a list of files that are well known and downloaded by many Internet Explorer users. If the file that you’re downloading isn’t on that list, SmartScreen Filter will warn you.

 
Anyway, that’s all I’ve got for now – Good Luck!
 

Errno::ECONNREFUSED in – Ruby on Rails

First problem of the morning – Errno::ECONNREFUSED in ProductsController#create. This one was actually a little bit tricky – for me anyway. The error seemed to imply a database issue, so I started by checking all the common problems there however no luck unfortunately.

After a bit of a Google I came across a post mentioning that a few people had encountered this after installing Solr. I had previously setup Sunspot, which just so happens to sit on top of Solr.

Manually starting solr fixed the problem – just using the code below:

chris@chris-VirtualBox:~/site$ rake sunspot:solr:start

 

I’m not really sure what triggered the error as I’d been using the exact same functionality all morning without any changes or issues. Thankfully it’s a quick fix. If anyone has anymore info on the problem please let me know.

Anyway, that’s all I’ve got for you for now, Good Luck!

UPDATE: I had this problem again this morning and realised that it was the models with search fields still defined that were triggering the error. Removing this code seems to have completely resolved the issue.

Truncating a String within a View – Ruby on Rails

Another quick post, just on how to truncate a string within a view. I used the following in order to ensure that my order_item title is never longer than 100 characters:

Title:  100, :seperator => ' ...') %>

 

This will output the title, if it’s longer than 100 characters the string will be shortened to 100 characters (minus the separator’s length). The neat thing about truncate is all the separator functionality is built in – you don’t have to throw any if/else logic in to check whether it’s required or not.

Output:

#Less than 100 characters
Title: lentesque porttitor

#More than 100 characters
Title: lentesque porttitor, velit vel ullamcorper pharetra, augue erat pharetra risus, quis bibendum fel…

Good Luck!

Adding a Link Within a Flash Notice/Message – Ruby on Rails

I came across an instance where I needed to add a link to flash notice tonight. Manually creating a hyperlink seems to work fine, however I was chasing a rubyish way of doing things:

flash[:notice] = "Order created - Click here to pay for it!"

 

Luckily a quick Google revealed the answer:

flash[:notice] = "Order created - Click here to pay for it!".html_safe

 

If you’re using rails 3 make sure you throw in the .html_safe or your link will be appear as plain text. Just a quick screenshot of the end result below:

Anyway, that’s all. Good Luck!

Passing Variables to a Partial from a View – Ruby on Rails

Just another one of those things that I find myself needing to do from time to time. Luckily this is all pretty straight forward (thankyou Ruby on Rails!). Assuming your partial is a form in the locations folder and that the variable you’re expecting is location all that you have to do is following:

View: users/show.html.erb

 'locations/form', :locals => { :location => @user.location } %>

Partial: locations/_form.html.erb


Street: 

Good luck!

Error 324 (net::ERR_EMPTY_RESPONSE) – Ruby on Rails

I managed to break my app again while playing around with the routes and a few redirects:

No data received
Unable to load the webpage because the server sent no data.
Here are some suggestions:
Reload this webpage later.
Error 324 (net::ERR_EMPTY_RESPONSE): The server closed the connection without sending any data.

 

This wasn’t one I had encountered before and unfortunately Google couldn’t give me anything to cheat off. Luckily, a quick review of the server log revealed the following message just before it closed the connection:

[2011-12-03 21:09:05] ERROR URI::InvalidURIError: the scheme http does not accept registry part: 192.168.1.3:3000locations (or bad hostname?)

 

From this error message it became clear that I needed to place a forward slash before my redirect path:

#Redirect to edit location path      
redirect_to '/locations/edit'

 

Hopefully this’ll be able to help someone else out there, Good Luck!

Validation Error Messages not Displaying – Ruby on Rails

Just another quick problem I ran into, the validation was indicating that an error had occurred however it did not show what the error was:

1 error prohibited this message from being saved:

— Usually an error would appear here —

Turns out the problem was pretty straight forward – it’s always the little things! I’d simply missed the each when displaying the error messages:


  
    

prohibited this message from being saved:

Simply adding the correct code fixed the problem straight away.

1 error prohibited this message from being saved:

Country can’t be blank

Good Luck!

No HTTP_REFERER was set in the request to this action, so redirect_to :back could not be called successfully

Well, I’ve managed to break my app again today – getting the following error message:

ActionController::RedirectBackError in OrdersController#new

No HTTP_REFERER was set in the request to this action, so redirect_to :back could not be called successfully. If this is a test, make sure to specify request.env[“HTTP_REFERER”].

 

Thankfully this is another one that’s fairly easy to fix. Simply create the following method – I’ve added mine to the application controller:

  #Redirect_to_back
  def go_back

    #Attempt to redirect
    redirect_to :back

    #Catch exception and redirect to root
    rescue ActionController::RedirectBackError
      redirect_to root_path
   end

 

You can then call this method from any of you controllers using the following code:

     #Redirect user to previous page
      go_back

 

You may get the following error depending on how the rest of your controller action is setup:

Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like “redirect_to(…) and return”.

 

This message is pretty self explanatory – simply add return immediately after your call to go_back in order to prevent any additional redirects being called:

#Redirect user to previous page
      go_back

      #Return to prevent multiple redirects
      return

 

Hopefully that’ll help someone out there, Good Luck!