0

I've found a helper method that I would like to use to resize embedded videos on my site. I've tried using this method several ways but received multiple undefined method errors. Here's the method:

def resize_video(new_width,new_height)
    width,height = embed_code.match(/width=.?(\d+).*height=.?(\d+)/).to_a.drop(1)
    embed_code.gsub(width,new_width).gsub(height,new_height)
end

I would like to apply this method to the <%= raw link.embed_code %> portion of my view, available HERE, to change the width and height to the desired values. Where should I put the method and how should it be called?

Update

Per Karel's advice, I put the method in links_helper.rb and used <%= raw (link.embed_code).resize_video %> in the view but received this error undefined method resize_video for #<String:0x492bf40>

2 Answers 2

1

I would suggest you to put the helper method in the corresponding helper of the view(ie. if the view file belongs a controller xyz, there should be a helper with name xyz_helper). This is the rails convention. If the helper method is used in multiple controller views, we can put it in application_helper.

If you are getting undefined method for embed_code, we have to pass that variable as follows

<%= raw resize_video(link.embed_code, width, height) %>
def resize_video(embed_code, new_width, new_height)
  width,height = embed_code.match(/width=.?(\d+).*height=.?(\d+)/).to_a.drop(1)
  embed_code.gsub(width,new_width).gsub(height,new_height)
end
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, now I'm getting this undefined method resize_video for #<#<Class:0x4a6bfb0>:0x4a69610> I've tried so many different ways, could this be a rails 3.1 issue?
I have not worked on rails 3.1 yet, but i don't think this could be a rails 3.1 issue. Most probably this could be because of broken code in the helper file or not probably loading the helper. If link is a model instance, then you may put the above code in a model method and call it from the view.
I winded up putting it directly in the controller, it's my first iteration and I'm not very specific about HOW it gets done. I just needed the feature. I used your modified function. Thanks!
0

Place your helper methods in a file name video_helper.rb in helpers folder. More here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.