Using sprees helpers within custom views - ruby-on-rails

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.

Related

How to use a rails helper in a PDF template inside services layer?

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 %>

Using helper methods in views

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.

How can i call the private method in controller by link_to in ruby on rails?

I have a method in my controller called scenario_population
def scenario_population
.....
end
I have a link_to method in view
<%= name_id %> #without form
<%= link_to 'Run', # %>
How can i call the controller method by link_to button here? I don't have created any routes for the private method in controller.
Please suggest me to get this? Thanks
Since all 'link_to' does is create a link element, it will need a url to put as the link's href. So what you're looking to do is impossible. You'll have to create a public route to access your controller method.

Using rails helper method

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.

render erb from database into view problem please help!

i am saving some erb in my database and rendering it in the view like this:
erb = ERB.new(content)
render :text => erb.result
I am getting errors when trying render erb that has the image_tag in the erb saved in the database. The error is :
undefined method `image_tag' for main:Object
Anyone help on this ? i also get the error with the stylesheet_link_tag ?
Thank alot
rick
I think that you would need to pass the optional binding parameter to the ERB::render method. This effectively provides the local variables in the scope of the ERB template. In other words the binding needs to provide the image_tag variable to the template.
I don't know what 'content' is in your case but the following will pass the binding from the 'parent' view assuming that #obj.image_tag is visible from that view:
<%= ERB.new("image tag - \<\%= #obj.image_tag \%\>").result(binding) %>
It's because you haven't helper in your controller. You need include all Helper do you use.

Resources