Posts Tagged ‘active_scaffold’

Getting Active_Scaffold to Work With Rails 2.3.2

Friday, May 15th, 2009

If you’re using Active_Scaffold and upgrading to Rails 2.3.2, here’s what worked for me.

  1. Update Rails to 2.3.2 (sudo gem update)
  2. Update Active_Scaffold to the latest version (script/plugin install –force git://github.com/activescaffold/active_scaffold.git)
  3. Install render_component plugin (script/plugin install git://github.com/lackac/render_component.git -r rails-edge)
  4. Start your application

Here are the resources which helped me:

Good luck!

Filtering Active_Scaffold in Rails Using Drop Down Box

Wednesday, February 11th, 2009

We use active_scaffold throughout the Marketing for Mavens application and love it. There are several instances where we wanted to filter the generated tables based on a selected item in a drop down box. Since we couldn’t find this process documented any where on the web, I’ve decided to highlight what we did to get this working.

First you’ll want to start with you drop down box. We used Rails observe_field to generate an onselect JavaScript action when an item was selected from the drop down box.

INDEX.HTML.ERB

<%= observe_field   :widget_widget_type_id,                       
        # The field to observe
:with => "'widget_type_id=' + escape(value)",                    
        # The input to validate
:on  => "onselect",                                               
        # The frequency in seconds to watch for changes
:url  => {:action => 'filter_widget', :controller => 'widgets' }, 
        # The action to call when changes occur
:update => :filter_widget
        # Name of the <div> to update
%>

<div id="filter_widget"><%= render :active_scaffold => 'widgets',
   :constraints => {:company_id => @company.id}, :condition => @condition %></div>

You will notice we created a <div> tag to hold the generated table. This is important because the filter will replace this code after the user selects an item from the drop down box, effectively filtering your results.

Now we need to update the widgets controller to handle the new “filter_widget” action.

WIDGETS_CONTROLLER.RB

def filter_widget
  @conditions = "widget_type_id=#{params[:widget_type_id]}"
  render :inline => "<%= render :active_scaffold => 'widgets',
     :constraints => {:company_id => #{@company.id}}, :conditions => @conditions %>"
end

def conditions_for_collection
  @conditions
end

condition_for_collection creates the initial conditions for the table. After a selection is made from the drop down, filter_widget is called and the <div> in the INDEX.HTML.ERB file is replaced with the new code. Now your table is filter accordingly.

When implementing this code you may receive an “ActionController::InvalidAuthenticityToken” error message. One fix for this is to use the following code in your controller.

protect_from_forgery :only => [:create, :update, :destroy]

This may make your application vulnerable to hackers so research the side effects before implementing to make sure it’s right for your environment.