Defining an action (or method) that filters DB data

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'

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     #- YouTube     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

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

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

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

Or 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