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>
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
Place your helper methods in a file name video_helper.rb in helpers folder. More here.
Related
I have a PDF template and I need to use a view helper method inside a HTML file that is in the services folder to generate the PDF. However, when I try to use the default view helpers, I get an exception that the method doesn't exist.
ActionView::Template::Error: undefined method `helper_method' for #<#<Class:0x000000000e309bd0>:0x000000000e3a0350>
How can I make this work?
You can make your module methods work as they were class methods by using
module_function :method
The code looks like this:
module Helper
def helper_method; end
module_function :helper_method
end
Then in your html.erb you do this:
<% Helper.helper_method %>
I'm having some trouble realising how the helper methods should be used in views. For example, take these parts of code:
Mycontrollers_helper.rb
module MycontrollersHelper
def destroy_everything
Model.destroy_all
redirect_to root_path
end
end
How should it be used in the view then ? Let's say adding the method to a button in the view:
<%= button_to 'Destroy all', destroy_everything, method => :post %>
Is just writing a method in the helper.rb file enough or does it require some additional lines in the controller it refers to ? Is this even the correct syntax for something like this ?
Helpers in rails actually view helpers. So they are meant to provide some help to render your views.
If you want to delete something, and then redirect to some action, just use a controller action for that.
I think you are taking about view helper, which you want to call from your view template.
You can call your view helper with the name of the method.
Calling destroy_everything will works fine if this helper is included in your controller.
Update:
If you write your helper method in application helper then you don't need to worry about load/ include the helper.
I have a helper with a method named search_form like this:
module Admin::BaseHelper
def search_form(*args)
# my great code here
end
end
To call this method in my HAML code, I can do this:
= search_form
= search_form()
= search_form(param1: "value1", param2: "value2"...)
My problem is with this first call. When I do this in any HAML file, it renders my helper. Except if my file name is _search_form.html.haml. Is that case, it returns nil.
If I put a raise error in the helper, I notice that my method isn't being called, but I am not able to find what is being called and why.
If I use the syntax on the second and third lines, it works as expected by calling my helper method.
So my question is: is this standard Rails behavior or a bug?
By default, Rails will look for a local variable with the same name as your partial, which may conflict with existing method names.
One way to get around this is to simply redefine the method inside your partial:
<% search_form = self.search_form %>
# Rest of the partial's code
i have a helper function which renders a partial and i pass a variable called method with it into the view...
when in view i use
<%= friend.method.profile.picture %>
the method variable can be either user or friend
and i get
wrong number of arguments(0 for 1)
i suppose there is a problem how i use the variable being passed into the association call... maybe i have to escape it somehow?
If I understand what you want, you are trying to dynamically call a function based on the value of a string argument called 'method'. Also, 'method' is an existing function in Ruby, (hence your error message about 'wrong number of args' vs 'undefined method'), so I would recommend renaming it in your code.
TLDR:
Rename your variable something like "person" (instead of 'method'), then
try some meta-programming to call the function using send:
friend.send(person).profile.picture
Here is the same answer as ~AmirRubin, but fleshed out more.
I am assuming that friend was the object, method was the helper, .profile is the method you want the helper to use.
Define your helper as:
def call_method(object, method_name)
object.send(method_name)
end
In your view call it as:
<%= call_method(friend, :profile).picture %>
Adjust this if my assumptions are wrong. If you need to send the method name (symbol) to the partial pass it in the locals.
I replaced the spree_application.html.erb template file with on of my
own but I like to use spree's helper methods such as
<%= breadcrumbs(#taxon) %>
<%= flash_messages %>
But I receive the following error:
undefined method `breadcrumbs' for #<#:0xb6525eac>
What should I do?
Thanks!
May be because the breadcrumbs method is in the taxons_helper so if you want to use it override the taxons_helper and add the breadcrumbs method to your application controller.