Actionlink response and ignore

7 Comments

Today I would like to show you how you may setup an action_link with the following features:

  • actionlink may be hidden for a record
  • showing success/error messages and updated record in response

We will use the example from my howto as a base.

Our task is to show a ‘fire’ action_link for players which are older than 6 years.

Note: We may have used existing authorization methods to accomplish this, however this is nt a question about authorization it s a simple logical decision.

1. Let s start by adding a ‘fire’ action_link to active scaffold’s config block of players controller:

active_scaffold :player do |conf|
  conf.action_links.add 'fire', :confirm => 'are_you_sure', :type => :member, :method => :put, :position => false
end

2. Fire is a new route to our players controller which means we have to add it to routes.rb in next step:

resources :players do
  as_routes
  put :fire, <code>:on</code> =>:member
end

3. Let s move on to write a fire method for players controller:

def fire
  @record = find_if_allowed(params[:id], :update)
  begin
    @record.update_attributes!(:salary => 0)
    self.successful = true
    flash[:info] = 'player fired'
  rescue
    self.successful = false
    flash[:error] = 'Internal Error'
  end

  respond_to_action(:action_update)
end

Last line includes a new feature: respond_to_action(:action_update), that code is responsible for updating the current record and showing any flash messages in your view.

You may start your application and you should see a fire action_link for each player s record.

4. Next step enables us to show ‘fire’ only if player is older than 6 years:

Firstly, we have to append an ignore_method parameter to our action link definition:

active_scaffold :player do |conf|
  conf.action_links.add 'fire', :confirm => 'are_you_sure', :type => :member, :method => :put, :position => false<strong>, :ignore_method => :ignore_fire_action?</strong>
end

Secondly, we have to implement ignore_fire_action? in our player’s controller:

protected
  
  def ignore_fire_action?(record = nil)
    if record
      record.date_of_birth.since(6.years).to_date < Date.today
    else
      true
    end
  end

Great, we ve finished our task and can relax for the rest of the day. 🙂

ActiveScaffold Using Dates with JQuery

17 Comments

Today I want to give you some detailed explanations about using Dates with JQuery in your Forms.

Activescaffold is using Datepicker from JQuery UI for Date Fields. unfortunately, Datepicker is not supporting Time Values, but a little Time Add-On removes that limitation.

If you want to try the following examples I would recommend to create a new rails 3 project with activescaffold according to my guide.

Afterwards please add the following migration to add some date and datetime columns:

  • rails generate migration AddDatesToTeams next_match_at:datetime founded_at:date last_relegation_at:date
  • rake db:migrate

Default Format

Out of the box Activescaffold is using the default date/time format in your locale file (../config/locale/..). You can adjust them as you like for example:

en:
  date:
    formats:
      default: "%d.%m.%Y"
  time:
    formats:
      default: "%d.%m.%Y %I:%M %p"

Please read the following limitations if you change these formats:

  • JQuery Datepicker requires a Date format to include day, month and year
  • JQuery Time Addon requires time to be after the date
  • %c, %U, %W, %w, %x, %X, %Z cannot be converted to jquery format

Default Options

If you take a look at JQuery UI Datepicker Options, you might wonder how to change default value of one of them.
Add the following to your locale file:

en:
  active_scaffold:
    date_picker_options:
      firstDay: 1

Same applies to time addon options:

en:
  active_scaffold:
    datetime_picker_options:
      showMinute: false

Format per column

You want to not want to use default format for a specific column ?
Try the following in activescaffold’s teams controller configuration:

conf.columns[:founded_at].options = {:format => :short}
conf.columns[:next_match_at].options = {:format => :short}

Options per column

Date/TimeAddon Options for a specific column are transferred to client via html node attributes.
Please take a look at the following examples:

conf.columns[:founded_at].options = {'date:firstDay' => 4}
conf.columns[:next_match_at].options = {'time:showMinute' => false}