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

On 11 April 2012 08:08, Soichi I. [email protected] wrote:

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

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

On 11 April 2012 09:44, Soichi I. [email protected] wrote:

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

You received this message because you are subscribed to the Google G. “Ruby
on Rails: Talk” group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.


gplus.to/clanlaw