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:

else

http://www.5valleys.com/

http://www.workingwithrails.com/person/8078

Hi

On 14 Apr., 22:18, Jon G. [email protected] wrote:

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

ah okay. And in my method I call :object instead of object when I
generate a label etc ?
So I convert to a symbol not when calling my helper in the view but in
my helper method.

On 15 Apr 2008, at 14:29, Martin wrote:

generate a label etc ?
So I convert to a symbol not when calling my helper in the view but in
my helper method.

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
Titel

But if I pass :post it is:
Titel

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

On 15 Apr 2008, at 16:54, Martin wrote:

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

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

Frederick C. wrote:

Well, you could get the object id, then use ObjectSpace to get the
symbol back…

Also as best I can tell (using /usr/dict/words as a sample set, I’ve not
looked at the source) Symbol#to_i is equivalent to Symbol#object_id /
10…

Both of which don’t have a whole lot of use… :wink:

Matt

On 15 Apr 2008, at 16:53, Martin wrote:

<%= make_input_row(f, @post, :title, “Titel”)%>

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

end

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

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)

The view
<% form_for(@post) do |f| %>
<%= make_input_row(f, @post, :title, “Titel”)%>
<%= f.submit “Create” %>
<% end %>

Would this be okay?

Thanks for your patience :wink:
Martin

On 15 Apr 2008, at 19:16, Martin 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

A symbol is just a name.

end
http://groups.google.de/group/rubyonrails-talk/browse_thread/thread/8a9d105037f0e7e0/e9f33293ef733130?lnk=gst&q=%40%40mandatory_fields#e9f33293ef733130
@@mandatory
if(object.class.mandatory.include?(method))

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