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.
Jun 13, 2011 @ 14:27:27
Is it possible to pass in data from the current record using dynamic parameters? For example, :dynamic_parameters => Proc.new {{:user_id => current_record.id}}
Jun 14, 2011 @ 07:07:11
if current_record is a helper method, yes sure.
Jun 15, 2011 @ 17:05:43
I was in need of a reference to the current row/record’s data. For example, the first row would generate dynamic parameters with user_id=1, second row would be user_id=2, etc.
I would still need a reference to the current record’s data, whether I was using a helper or not. After some digging through the request data and variable inspection, I was able to get a reference to what I needed. However, I’m not sure if it’s the best/cleanest way to reference this. Here is what I ended up with:
:dynamic_parameters => Proc.new {{:user_id => @view_stack[4][:locals][:record].id}}
Jun 15, 2011 @ 17:20:47
Just an alternativ:
Proc.new {{:user_id => @link_record.id}}