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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
If you''ve defined @book in your controller action, then just pass that. eg <%= printrow(@book, :title) %>. Martin wrote:> 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 > > > > >-- http://www.5valleys.com/ http://www.workingwithrails.com/person/8078 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi On 14 Apr., 22:18, Jon Garvin <jgarvin.li...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 15 Apr 2008, at 14:29, Martin wrote:> > Hi > > On 14 Apr., 22:18, Jon Garvin <jgarvin.li...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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.no, your printrow method would be unchanged. Fred --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
By the way: Isn''t there any way to access the object a symbol is pointing to? Martin --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 15 Apr 2008, at 16:53, Martin wrote:> > 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> > end> > > Martin > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> 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. > >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.... ;-) Matt --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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")) > endokay. 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 ;-) Martin --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 seemsA 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 :-) You can get it out of formular (formular.object). Other than that seems reasonable enough. Fred> 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 ;-) > Martin > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---