A real quick one for today.
We will take a closer look at which options you have to pass addional url params to action_links.
My example is based upon my post: ActiveScaffold Confirmation of Actionlinks
Static Parameters
A static parameter is a simple key-value pair inside of a hash you may define for your actionlink:
class PlayersController < ApplicationController
active_scaffold :player do |conf|
....
conf.action_links.member.add 'fire', :confirm => 'are_you_sure', :type => :member, :method => :put, :position => false, :ignore_method => :ignore_fire_action?, :parameters => {:static => 'static'}
....
end
end
Reload players controller in your browser and you see the new parameter ‘static’ attached to each fire action_link.
Dynamic Parameters
Static Parameters are nice, but well they are static.
However, it s also possible to do dynamic one s a s well and actually it s quite easy.
class PlayersController < ApplicationController
active_scaffold :player do |conf|
....
conf.action_links.add 'fire', :confirm => 'are_you_sure', :type => :member, :method => :put, :position => false, :ignore_method => :ignore_fire_action?, :parameters => {:static => 'static'}, :dynamic_parameters => Proc.new {{:dynamic => random_number}}
....
end
end
We ve added the following to our action_link definition: :dynamic_parameters => Proc.new {{:dynamic => random_number}}
A simple Proc Object which is returning a hash including a key ‘dynamic’ with a value… random_number…. ??
Let me explain: our Proc object is called in scope of your helpers, which means you have access to any helper methods.
Therefore we define a helper method ‘random_number’ in our players_helper:
players_helper.rb
module PlayersHelper
def random_number
400 + rand(100)
end
end
Wish you a great weekend.