In Rails, how do I assign an instance variable from within define_method and expose it from controllers down to views?
I'm trying to write an action generator for my Rails apps (probably the most basic form of metaprogramming you can get. My question is, how do I assign an instance variable that is available in views? To me, the code would look like this:
ApplicationController < ActionController::Base
def generate_custom_action
define_method("my_custom_action") do
#...some code...
instance_variable_set("@variable_name", my_value)
# OR
@variable_name = 'aoeu'
end
end
end
But that doe开发者_StackOverflowsn't seem to work. That is, the varable "@variable_name" is 'nil' in the views. Any idea how to expose this to the view?
You have a method that defines some vars. And the @vars
are available everywhere in the class instance.
So doing :
def generate_custom_action
@variable_name = my_value
end
Should be enough for you to get the value in your view.
精彩评论