Hi there, I''m kind of a newbie on RoR and on this group and I was wondering if someone could help to clean this bit of code: <%= link_to_remote (image_tag("hide.png", :size=>"16x16"), :url=>"/message/#{message.id}/hide", :method=>"post", :before=>"Effect.Fade(this.parentNode.parentNode, {duration: 0.5}); Effect.BlindUp(this.parentNode.parentNode, {duration: 0.5})") %> <%= link_to_remote (image_tag("top.png", :size=>"16x16"), :url=>"/message/#{message.id}/top", :method=>"post", :before=>"Effect.Fade(this.parentNode.parentNode, {duration: 0.5}); Effect.BlindUp(this.parentNode.parentNode, {duration: 0.5})") %> <%= link_to_remote (image_tag("bottom.png", :size=>"16x16"), :url=>"/message/#{message.id}/bottom", :method=>"post", :before=>"Effect.Fade(this.parentNode.parentNode, {duration: 0.5}); Effect.BlindUp(this.parentNode.parentNode, {duration: 0.5})") %> As you can see I have some kind of options that do repeat themselves. My idea was to find a way to describe them for once and use them repeatedly. Any thoughts on how to achieve this, Thanks in advance, Christophe -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> > I''m kind of a newbie on RoR and on this group and I was wondering if > someone could help to clean this bit of code: >How about something as simple as: <% for image_filename in ["hide.png", "top.png", "bottom.png"] %> <%= link_to_remote (image_tag(image_filename, :size=>"16x16"), :url=>"/message/#{message.id }/hide", :method=>"post", :before=>"Effect.Fade(this.parentNode.parentNode, {duration: 0.5}); Effect.BlindUp(this.parentNode.parentNode, {duration: 0.5})") %> <% end %> Alternatively, make a helper method and call that each time. Cheers, Andy -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thanks, That''s a good start. Any advice where to start looking for how to make a helper method ?> I''m kind of a newbie on RoR and on this group and I was wondering if > someone could help to clean this bit of code: > > How about something as simple as: > > <% for image_filename in ["hide.png", "top.png", "bottom.png"] %> > <%= link_to_remote (image_tag(image_filename, :size=>"16x16"), > :url=>"/message/#{message.id}/hide", > :method=>"post", > :before=>"Effect.Fade(this.parentNode.parentNode, {duration: > 0.5}); > Effect.BlindUp(this.parentNode.parentNode, {duration: 0.5})") > %> > <% end %> > > Alternatively, make a helper method and call that each time. > > Cheers, > > > Andy > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 9 March 2010 13:30, Christophe Decaux <christophe.decaux-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote:> Thanks, > That''s a good start. > > Any advice where to start looking for how to make a helper method ? >I''d recommend Agile Web Development with Rails (Pragmatic Programmers book). I looked online but couldn''t see a lot that was easy to learn from. Basically a helper method is a method that is available in your views and (can be) used for generating markup that would be repetitive otherwise. For example you could have something this: application_helper.rb: module ApplicationHelper def christophe_link_action(image_filename) link_to_remote (image_tag(image_filename, :size=>"16x16"), :url=>"/message/#{message.id }/hide", :method=>"post", :before=>"Effect.Fade(this.parentNode.parentNode, {duration: 0.5}); Effect.BlindUp(this.parentNode.parentNode, {duration: 0.5})") end end Then in your view just do: <%= christophe_link_action("foo.png") %> <%= christophe_link_action("bar.png") %> Does that make things any clearer? If not, I''d recommend the book I mentioned earlier (or post back with questions). Cheers, Andy -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Only thing to note it that the :url parameter changes on each line (it does not always end with ''hide''!) The fact that it is the same as the image name should be treated as an unfortunate coincidence. The christophe_link_action() method should take two parameters, one for the image file name and one for the url (or at least the last part of it). -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thanks, This is good concise explanation. I do have the book you suggests, but haven''t had the time to go deep enough in it. Furthermore, I went back to it to check what it says about helper creation, but didn''t quite understood Cheers, Christophe Le 9 mars 2010 à 15:08, Andy Jeffries a écrit :> On 9 March 2010 13:30, Christophe Decaux <christophe.decaux-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > Thanks, > That''s a good start. > > Any advice where to start looking for how to make a helper method ? > > I''d recommend Agile Web Development with Rails (Pragmatic Programmers book). I looked online but couldn''t see a lot that was easy to learn from. > > Basically a helper method is a method that is available in your views and (can be) used for generating markup that would be repetitive otherwise. > > For example you could have something this: > > application_helper.rb: > module ApplicationHelper > def christophe_link_action(image_filename) > link_to_remote (image_tag(image_filename, :size=>"16x16"), >> :url=>"/message/#{message.id}/hide", >> :method=>"post", >> :before=>"Effect.Fade(this.parentNode.parentNode, {duration: >> 0.5}); >> Effect.BlindUp(this.parentNode.parentNode, {duration: 0.5})") > end > end > > Then in your view just do: > > <%= christophe_link_action("foo.png") %> > <%= christophe_link_action("bar.png") %> > > Does that make things any clearer? If not, I''d recommend the book I mentioned earlier (or post back with questions). > > Cheers, > > > Andy > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Agreed. I didn''t notice that in the OP''s code. Cheers, Andy -- Andy Jeffries http://andyjeffries.co.uk/ #rubyonrails #mysql #jquery Registered address: 64 Sish Lane, Stevenage, Herts, SG1 3LS Company number: 5452840 On 9 March 2010 14:20, Peter Hickman <peterhickman386-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> Only thing to note it that the :url parameter changes on each line (it does > not always end with ''hide''!) > > The fact that it is the same as the image name should be treated as an > unfortunate coincidence. The christophe_link_action() method should take > two parameters, one for the image file name and one for the url (or at least > the last part of it). > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Christophe wrote:> Hi there, > > I''m kind of a newbie on RoR and on this group and I was wondering if > someone could help to clean this bit of code: > > <%= link_to_remote (image_tag("hide.png", :size=>"16x16"), > :url=>"/message/#{message.id}/hide", > :method=>"post", > :before=>"Effect.Fade(this.parentNode.parentNode, {duration: > 0.5}); > Effect.BlindUp(this.parentNode.parentNode, {duration: > 0.5})") > %> > <%= link_to_remote (image_tag("top.png", :size=>"16x16"), > :url=>"/message/#{message.id}/top", > :method=>"post", > :before=>"Effect.Fade(this.parentNode.parentNode, {duration: > 0.5}); > Effect.BlindUp(this.parentNode.parentNode, {duration: > 0.5})") > %> > <%= link_to_remote (image_tag("bottom.png", :size=>"16x16"), > :url=>"/message/#{message.id}/bottom", > :method=>"post", > :before=>"Effect.Fade(this.parentNode.parentNode, {duration: > 0.5}); > Effect.BlindUp(this.parentNode.parentNode, {duration: > 0.5})") > %> > > As you can see I have some kind of options that do repeat themselves. > My idea was to find a way to describe them for once and use them > repeatedly. > Any thoughts on how to achieve this,Well, one thing to do: take the JavaScript out of your ERb files and put it in a separate JavaScript file. This has many advantages, among them the fact that it''s easier to reduce repetition.> > Thanks in advance, > > ChristopheBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.