In at least two places I want to display radio buttons for choosing a persons gender. Now, with radiation levels rising and everything, I want to be safely guarded for future changes to basic biology, such as a third sex. And, anyway, I try to heed the admonition not to repeat myself. A concrete fragment from an existing view looks like this <fieldset> <legend>Gender</legend> <label for="gender_female">Female</label> <%= radio_button "person", "gender", "F", "id" => "gender_female" %> <label for="gender_male">Male</label> <%= radio_button "person", "gender", "M", "id" => "gender_male" %> </fieldset> What I don''t understand, yet, is how to parameterize and where to put the fragment so that I can call it the same way as other helpers, i.e. as gender_choice("person", "gender"). Maybe implementing a "component" like this as a method isn''t even advisable and there are better ways. Michael -- Michael Schuerig All good people read good books mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org Now your conscience is clear http://www.schuerig.de/michael/ --Tanita Tikaram, Twist In My Sobriety
Gavin Kistner
2005-Apr-18 12:47 UTC
Re: How to write and where to put my own view helpers?
On Apr 18, 2005, at 6:36 AM, Michael Schuerig wrote:> What I don''t understand, yet, is how to parameterize and where to put > the fragment so that I can call it the same way as other helpers, i.e. > as gender_choice("person", "gender").Methods defined inside the ApplicationHelper module at app/helpers/application.rb are made available inside all views. Enjoy!
David Goodlad
2005-Apr-18 12:59 UTC
Re: How to write and where to put my own view helpers?
On 4/18/05, Michael Schuerig <michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org> wrote:> > In at least two places I want to display radio buttons for choosing a > persons gender. Now, with radiation levels rising and everything, I > want to be safely guarded for future changes to basic biology, such as > a third sex. And, anyway, I try to heed the admonition not to repeat > myself. > > A concrete fragment from an existing view looks like this > > <fieldset> > <legend>Gender</legend> > <label for="gender_female">Female</label> > <%= radio_button "person", "gender", "F", > "id" => "gender_female" %> > <label for="gender_male">Male</label> > <%= radio_button "person", "gender", "M", > "id" => "gender_male" %> > </fieldset> > > What I don''t understand, yet, is how to parameterize and where to put > the fragment so that I can call it the same way as other helpers, i.e. > as gender_choice("person", "gender"). > > Maybe implementing a "component" like this as a method isn''t even > advisable and there are better ways. > > Michael > > -- > Michael Schuerig All good people read good books > mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org Now your conscience is clear > http://www.schuerig.de/michael/ --Tanita Tikaram, Twist In My Sobriety > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >Michael, This situation is probably better suited to the use of partials instead of helpers. What a lot of people do is create a ''shared'' directory in their views directory, then add files like _gender.rhtml as a partial that can be rendered as: render_partial ''shared/gender'', {:person => person, :gender => gender} Hope that helps Dave -- Dave Goodlad dgoodlad-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org or dave-eHfbeeWWzZOw5LPnMra/2Q@public.gmane.org http://david.goodlad.ca/
Michael Schuerig
2005-Apr-18 13:37 UTC
Re: How to write and where to put my own view helpers?
On Monday 18 April 2005 14:59, David Goodlad wrote:> On 4/18/05, Michael Schuerig <michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org> wrote:[component for gender]> This situation is probably better suited to the use of partials > instead of helpers. What a lot of people do is create a ''shared'' > directory in their views directory, then add files like _gender.rhtml > as a partial that can be rendered as: > > render_partial ''shared/gender'', {:person => person, :gender => > gender}Thanks! That''s it with a slight syntactic change render_partial ''shared/gender'', nil, :person => person, :gender => gender From the wiki I found this page http://www.pointstorm.com/~gavin/partials-doc/classes/ActionView/Partials.html which was helpful for the details. Michael -- Michael Schuerig I am the sum total of the parts mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org I control directly. http://www.schuerig.de/michael/ --Daniel C. Dennett, Elbow Room
David Goodlad
2005-Apr-18 13:42 UTC
Re: Re: How to write and where to put my own view helpers?
On 4/18/05, Michael Schuerig <michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org> wrote:> On Monday 18 April 2005 14:59, David Goodlad wrote: > > On 4/18/05, Michael Schuerig <michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org> wrote: > [component for gender] > > > This situation is probably better suited to the use of partials > > instead of helpers. What a lot of people do is create a ''shared'' > > directory in their views directory, then add files like _gender.rhtml > > as a partial that can be rendered as: > > > > render_partial ''shared/gender'', {:person => person, :gender => > > gender} > > Thanks! That''s it with a slight syntactic change > > render_partial ''shared/gender'', nil, > :person => person, :gender => gender > > From the wiki I found this page > http://www.pointstorm.com/~gavin/partials-doc/classes/ActionView/Partials.html > which was helpful for the details. > > Michael > > -- > Michael Schuerig I am the sum total of the parts > mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org I control directly. > http://www.schuerig.de/michael/ --Daniel C. Dennett, Elbow Room > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >Oops, yes, sorry. Just got up this morning, and on my mind was the content of a changeset to Rails that I saw yesterday that plays with how the assignments are made in partials; the line that you''ve posted should do the trick. Dave -- Dave Goodlad dgoodlad-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org or dave-eHfbeeWWzZOw5LPnMra/2Q@public.gmane.org http://david.goodlad.ca/
Gavin Sinclair
2005-Apr-18 23:53 UTC
Re: Re: How to write and where to put my own view helpers?
On Monday, April 18, 2005, 11:37:53 PM, Michael wrote:>> render_partial ''shared/gender'', {:person => person, :gender => >> gender}> Thanks! That''s it with a slight syntactic change> render_partial ''shared/gender'', nil, > :person => person, :gender => genderShould be render_partial ''shared/gender'', nil, ''person'' => person, ''gender'' => gender I think.>>From the wiki I found this page > http://www.pointstorm.com/~gavin/partials-doc/classes/ActionView/Partials.html > which was helpful for the details.Partials are being simplified in the next version of Rails. That documentation you found will be modified to match the changes and included in the source. Under the new code, you can do: render_partial ''shared/gender'', :person => person, :gender => gender i.e. no need for that nasty ''nil''; and you can use symbols for the local variable names. Gavin