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@googlegroups.com.
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.

Search Discussions

  • Colin Law at Apr 11, 2012 at 8:29 am

    On 11 April 2012 08:08, Soichi Ishida 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@googlegroups.com.
    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 at Apr 11, 2012 at 8:45 am
    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@googlegroups.com.
    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.
  • Colin Law at Apr 11, 2012 at 8:58 am

    On 11 April 2012 09:44, Soichi Ishida 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]
    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

    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@googlegroups.com.
    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@googlegroups.com.
    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.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedApr 11, '12 at 7:08a
activeApr 11, '12 at 8:58a
posts4
users2
websiterubyonrails.org
irc#RubyOnRails

2 users in discussion

Soichi Ishida: 2 posts Colin Law: 2 posts

People

Translate

site design / logo © 2021 Grokbase