converting symbol to object

Hi,

writing a helper I pass a symbol of my model-object (something like <%= printrow(:book, :title) =>)

In the helper-method I need to do something like this:

def printrow(object, method)    if object.somemethod      output    else      otheroutput    end end

My problem is that object is a symbol and not an object of my model Book. So how do I convert the symbol to my object?

Thanks, Martin

If you've defined @book in your controller action, then just pass that.
eg <%= printrow(@book, :title) %>.

Martin wrote:

Hi

no, your printrow method would be unchanged.

Fred

Hi Fred,

let form_helper.rb be

module FormHelper   def make_input_row(formular, object, method, text)     make_row(formular, object, method, text, formular.text_field(method))   end

  def make_row(formular, object, method, text, html = "")     content_tag("div",       content_tag("div", label( object, method, text) ) +         html +         content_tag("div", "", :class => "clear"))   end end

and new.html.erb contain <% form_for(@post) do |f| %>   <%= make_input_row(f, @post, :title, "Titel")%>   <%= f.submit "Create" %> <% end %>

Then the output contains <label for="__Post:0x313f080_title">Titel</label></div><input id="post_title" name="post[title]" size="30" type="text" />

But if I pass :post it is: <label for="post_title">Titel</label></div><input id="post_title" name="post[title]" size="30" type="text" />

So I guess if I pass @post I have to change my make_input_row to   def make_input_row(formular, object, method, text)     make_row(formular, :object, method, text, formular.text_field(method))   end

Martin

By the way: Isn't there any way to access the object a symbol is pointing to? Martin

Yes and no. Yes in that you can use instance_variable_get to retrieve a named instance variable for an object, no in that a symbol doesn't point at any object. it's just a symbol.

Fred

Hi Fred,

let form_helper.rb be

module FormHelper def make_input_row(formular, object, method, text)    make_row(formular, object, method, text, formular.text_field(method)) end

def make_row(formular, object, method, text, html = "")    content_tag("div",      content_tag("div", label( object, method, text) ) +        html +        content_tag("div", "", :class => "clear")) end end

and new.html.erb contain <% form_for(@post) do |f| %> <%= make_input_row(f, @post, :title, "Titel")%> <%= f.submit "Create" %> <% end %>

Then the output contains <label for="__Post:0x313f080_title">Titel</label></div><input id="post_title" name="post[title]" size="30" type="text" />

But if I pass :post it is: <label for="post_title">Titel</label></div><input id="post_title" name="post[title]" size="30" type="text" />

So I guess if I pass @post I have to change my make_input_row to def make_input_row(formular, object, method, text)    make_row(formular, :object, method, text, formular.text_field(method))

That is entirely useless. :object does not (your later email seems to
indicate your think it does) have any association with the local
variable object. The reason the object/method things works at all is that by convention
instance variables are uses (and then instance_variable_get comes to
the rescue). in your particular case, you don't need it at all though

  def make_input_row(formular, method, text)     make_row(formular, method, text,formular.text_field(method))    end

  def make_row(formular, method, text, html = "")     content_tag("div",       content_tag("div", formular.label(method, text) ) + html +         content_tag("div", "", :class => "clear"))   end

Fred

Frederick Cheung wrote:

Hi Fred,

That is entirely useless. :object does not (your later email seems to indicate your think it does) have any association with the local variable object.

it's very confusing for me. But your guess was right. I thought a symbol is something like a pointer. But that's not correct it seems

The reason the object/method things works at all is that by convention instance variables are uses (and then instance_variable_get comes to the rescue). in your particular case, you don't need it at all though

  def make_input_row(formular, method, text)     make_row(formular, method, text,formular.text_field(method))    end

  def make_row(formular, method, text, html = "")     content_tag("div",       content_tag("div", formular.label(method, text) ) + html +         content_tag("div", "", :class => "clear"))   end

okay. I hope I understand. My original idea was to do something like that I asked for in that thread http://groups.google.de/group/rubyonrails-talk/browse_thread/thread/8a9d105037f0e7e0/e9f33293ef733130?lnk=gst&q=%40%40mandatory_fields#e9f33293ef733130

So I tried to break it down to what I thought was essential. So let me please show you the way I would do it now:

The model Post contains: class Post < ActiveRecord::Base   @@mandatory = [:title]   validates_presence_of @@mandatory

  def self.mandatory     @@mandatory   end end

My helper:   def make_input_row(formular, object, method, text)     make_row(formular, object, method, text, formular.text_field(method))   end

  def make_row(formular, object, method, text, html = "")     if(object.class.mandatory.include?(method))       mandatorytag = "*"     else       mandatorytag = ""     end     content_tag("div",       content_tag("div", formular.label(method, text) ) + mandatorytag

Hi Fred,

That is entirely useless. :object does not (your later email seems to indicate your think it does) have any association with the local variable object.

it's very confusing for me. But your guess was right. I thought a symbol is something like a pointer. But that's not correct it seems

A symbol is just a name.

The reason the object/method things works at all is that by
convention instance variables are uses (and then instance_variable_get comes to the rescue). in your particular case, you don't need it at all though

def make_input_row(formular, method, text)    make_row(formular, method, text,formular.text_field(method))   end

def make_row(formular, method, text, html = "")    content_tag("div",      content_tag("div", formular.label(method, text) ) + html +        content_tag("div", "", :class => "clear")) end

okay. I hope I understand. My original idea was to do something like that I asked for in that thread http://groups.google.de/group/rubyonrails-talk/browse_thread/thread/8a9d105037f0e7e0/e9f33293ef733130?lnk=gst&q=%40%40mandatory_fields#e9f33293ef733130

So I tried to break it down to what I thought was essential. So let me please show you the way I would do it now:

The model Post contains: class Post < ActiveRecord::Base @@mandatory = [:title] validates_presence_of @@mandatory

def self.mandatory    @@mandatory end end

My helper: def make_input_row(formular, object, method, text)    make_row(formular, object, method, text, formular.text_field(method)) end

def make_row(formular, object, method, text, html = "")    if(object.class.mandatory.include?(method))      mandatorytag = "*"    else      mandatorytag = ""    end    content_tag("div",      content_tag("div", formular.label(method, text) ) + mandatorytag +        html +        content_tag("div", "", :class => "clear")) end

I need to pass object again to get the mandatory field(s)

You don't :slight_smile: You can get it out of formular (formular.object). Other
than that seems reasonable enough.

Fred