Rails 3.1.3 I would like to define a method that takes one of DB fields as an input and returns a portion of it. A table ''Project'' has a field URL. I need to output a portion of the URL. Say, I have a YouTube URL, and the output must be only ''WXYZ'' http://www.youtube.com/watch?v=WXYZ so I have tried to define an action in ''projects_controller.rb'', helper_method :video_code def video_code url return getVideoCode(url) end def getVideoCode url #http://www.youtube.com/watch?v=Z_LPKEPReUo re = Regexp.new(''http:\/\/www.youtube.com\/watch\?v=(.*)'') m = re.match(url) return m[1] end In (project)index.html.erb <%= project.video_code(project.url) %> gives an error, undefined method `video_code'' for #<ProjectsController:0x000001035f64d8> I think the way I defined the action (or method) is wrong. Could anyone help me for this? Thanks in advance. soichi -- 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.
On 11 April 2012 08:08, Soichi Ishida <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Rails 3.1.3 > > I would like to define a method that takes one of DB fields as an input > and returns a portion of it. > > A table ''Project'' has a field URL. I need to output a portion of the > URL. > > Say, I have a YouTube URL, and the output must be only ''WXYZ'' > > http://www.youtube.com/watch?v=WXYZ > > so I have tried to define an action in ''projects_controller.rb'', > > helper_method :video_code > def video_code url > return getVideoCode(url) > end > > > def getVideoCode url > #http://www.youtube.com/watch?v=Z_LPKEPReUo > re = Regexp.new(''http:\/\/www.youtube.com\/watch\?v=(.*)'') > m = re.match(url) > return m[1] > end > > > In (project)index.html.erb > > <%= project.video_code(project.url) %> > > gives an error, > > > undefined method `video_code'' for #<ProjectsController:0x000001035f64d8> > > I think the way I defined the action (or method) is wrong.You should make video_code a method of the Project model class. So inside project.rb put the method video_code. There will be no need to pass a parameter as it can access the url method of itself directly and return the required string. You will not need getVideoCode either. Then in the view you can just use project.video_code. Colin -- 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.
Soichi Ishida
2012-Apr-11 08:44 UTC
Re: Defining an action (or method) that filters DB data
Thanks . I put def video_code return getVideoCode(self.url) end def getVideoCode url re = Regexp.new(''http:\/\/www.youtube.com\/watch\?v=(.*)'') m = re.match(url) return m[1] end in project.rb, and it works perfectly well! soichi -- 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.
Colin Law
2012-Apr-11 08:57 UTC
Re: Re: Defining an action (or method) that filters DB data
On 11 April 2012 09:44, Soichi Ishida <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Thanks . I put > > def video_code > return getVideoCode(self.url) > end > > def getVideoCode url > re = Regexp.new(''http:\/\/www.youtube.com\/watch\?v=(.*)'') > m = re.match(url) > return m[1] > endOr just def video_code re = Regexp.new(''http:\/\/www.youtube.com\/watch\?v=(.*)'') m = re.match(self.url) return m[1] end If you want to keep getVideoCode then ideally it should be called get_video_code to follow the method naming conventions. Colin> > > in project.rb, and it works perfectly well! > > soichi > > -- > 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@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- gplus.to/clanlaw -- 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.