views:

51

answers:

1

Hi there, 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 doesn't seem to work. That is, the varable "@variable_name" is 'nil' in the views. Any idea how to expose this to the view?

A: 

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.

Damien MATHIEU
oops...I had forgotten to add the "define_method" part to the example above...I've added it now though. I see what you're saying though, and it looks like it should work...maybe I was just overthinking. I'll try it and let you know.
btelles