Create Button which does not Submit – Ruby on Rails

Just another quick post – how to create a button which does not submit a form when it’s clicked:

 'button', :onclick => 'alert('Button not Submitted - Hopefully!!!!'), :class => "my_class" %>

Let me know if you have any problems.

Devise Overriding Controller Route – Ruby on Rails

I ran into a bit of a problem today after implementing a search form that appeared on every page. When the login page was loaded I received the following error:

No route matches {:controller=>”devise/products”, :method=>:get, :action=>”search”}

The weird thing about this is that the route my search form uses does not mention devise in any way:

 'products', :action => 'search', :method => :get do %>

As you have probably already guessed, I’m using devise to handle my authentication. Unfortunately this seems to be overriding some of my routes whenever it co-exists with another form. Luckily there exists a very simple, although hard to find solution – simply add a forward slash before the controller definition:

 '/products', :action => 'search', :method => :get do %>

Here’s a quick link to the StackOverflow post which gave the answer, hopefully a few more links to it will make it a little easier to find for anyone else out there doing a search!

Re-Index Sunspot/Solr – Ruby on Rails

Just a very quick post on how to re-index Sunspot/Solr – simply open the console and enter the following command:

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

WARNING: 'task :t, arg, :needs => [deps]' is deprecated.  Please use 'task :t, [args] => [deps]' instead.
    at /usr/lib/ruby/gems/1.8/gems/sunspot_rails-1.2.1/lib/sunspot/rails/tasks.rb:41
chris@chris-VirtualBox:~/site$ 

That’s all there is to it, nice and easy!

Will_Paginate with Sunspot – Ruby on Rails

Hey all,

I’ve just gone through setting up will_paginate with sunspot 1.3, just thought I’d share in case anyone else has any troubles/improvements.

In my case I’m setting it up on a products model.

  #products.rb

  #Sunspot/Search
  searchable do
    text :title, :boost => 5
    text :description
  end

#products_controller.rb
  #Products search
  def search

    #Create vars
    search = Product.search do
      fulltext params[:search]
      paginate :page => params[:page] || 1, :per_page => 2
    end    

    #Retrieve results
    @products = search.results

    #Create responses
    respond_to do |format|
      format.html { render 'index' }
    end
  end

#Search partial - _search.html.erb
 'products', :action => 'search', :method => :get do %>
    '25', :class => 'search' %>

#Product display page - index.html.erb

    "products/product_line", :locals => {:product => product} %>

      
 false, :params => { :search_text => params[:search_text] } %>

Well that’s about it, if anyone needs any further info or has any tips, leave a comment and I’ll get back to you asap.

Uninstalling a Gem – Ruby on Rails

Due to a few recent issues with SunSpot (see my previous posts), I’ve decided to uninstall it until I have enough time to work on it properly. Once again, Ruby makes this pretty simple:


chris@chris-VirtualBox:~/site$ sudo gem uninstall sunspot
[sudo] password for chris: 

You have requested to uninstall the gem: sunspot-1.2.1 sunspot_rails-1.2.1 depends on [sunspot (= 1.2.1)]
If you remove this gems, one or more dependencies will not be met.
Continue with Uninstall? [Yn]  y


Remove executables: sunspot-solr, sunspot-installer in addition to the gem? [Yn]  y


Removing sunspot-solr
Removing sunspot-installer


Successfully uninstalled sunspot-1.2.1

ActiveRecord::HasManyThroughAssociation NotFoundError – Ruby on Rails

I ran into a bit of a problem with a has_many relationship in rails today:

ActiveRecord::HasManyThroughAssociationNotFoundError in FeedbacksController#index

Could not find the association :sub_orders in model User

This turned out to be a fairly simple fix, I had forgotten to add a has_many relationship before adding the has_many :through => x relationship. There is a fairly detailed explanation on StackOverflow for anyone who is curious.

undefined method `to_sym’ for nil:NilClass – Ruby on Rails Migration

While trying to do a migration today I received the following message:

undefined method `to_sym’ for nil:NilClass

#After running a trace (rake db:migrate –trace)
undefined method `to_sym’ for nil:NilClass
/usr/lib/ruby/gems/1.8/gems/activesupport-3.1.1/lib/active_support/whiny_nil.rb:48:in `method_missing’

This was the migration:

class AddDefaultValuesToFeedbacks  0
    change_column :stores, :rating, :decimal, :precision => 8, :scale => 2, :default => 0
  end
end

Unfortunately this problem also prevented me from performing any other migrations on the table. I attempted deleting all relevant migration files and then re-running them, restarting the server and rolling back to a previous migration – none of which worked.

Eventually I resorted to exporting the dataset, dropping the table via sqlite3 and recreating it. Thankfully this seems to have worked. The only hint I’ve come across that may explain the cause of this problem, other than typos or referring to non-existant columns, is that it can occur if a migration is screwed over before it finishes and fails to rollback properly.

If anyone else has any concrete answers, please let me know in the comments!

Debugging with Exceptions – Ruby on Rails

Ran into a bit of trouble with a model today, after a bit of a google I came across this technique which helped me solve it:

#Raise exception on object.inspect
raise Object.inspect

#Example 1
raise order.inspect

#Example 2
raise [sub_orders.count].inspect

This simply allows you display variable values as an exception. By placing a few of these throughout your troublesome code you can simulate a fully targeted trace.

Submit form_for to Custom Action – Ruby on Rails

Just another quick problem I ran into with rails this afternoon – how to submit a form_for to a custom action. Luckily there’s quite a bit on how to do this in the documentation, my solution ended up as follows:

 url_for(:controller => 'feedbacks', :action => 'leave_seller_feedback') do |f| %>

Just substitute your form_for tag with the one above and put in the relevant details and it should all work fine. Keep in mind that the submit tag doesn’t need to be changed, just the form_for tag.