Can I use helpers from actions? - symfony1

I'm trying to use helpers from actions in this way:
sfContext::getInstance()->getConfiguration()->loadHelpers(array('I18N', 'Asset', 'Url'));
echo image_tag('bullet_red.png');
echo link_to('Ver detalles', 'ver-detalles/' . $record->getIdregistros(), 'class="btn btn-success btn-mini"');
But I get this error:
Fatal error: Call to undefined function _parse_attributes() in
/var/www/html/monitor/lib/vendor/symfony/lib/helper/AssetHelper.php on
line 333
Why?

_parse_attributes() function is located in the Tag Helper.
so you will have to import the Tag Helper as well
sfContext::getInstance()->getConfiguration()->loadHelpers(array('I18N', 'Asset', 'Url', 'Tag'));

Related

rails how to use helpers with ERB.new in service?

I have a service that uses an ERB file to render a template form.
In the form I use several helpers, but they don't seem to be available in this context.
how could I use my helpers in the rendering via ERB.new(template).result(binding) ?
this is the error I'm getting:
*** NoMethodError Exception: undefined method `image_encoded' for #<MyService>
here's the call in the service app/services/my_service.rb file:
ERB.new(template).result(binding)
here's my helper app/helpers/my_helper.rb:
module MyHelper
def image_encoded(image_url)
<<image stuff>>
end
end
here's my call to the helper in the app/views/my_template/my_template.html.erb file:
<img src="<%= image_encoded(image) %>"/>
In your service your helpers are not included by default. You can include the methods from a particular helper like this:
class MyService
include MyHelper
end

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

Named route helper in javascript

If I'm doing some ajax stuff, I can get where a form 'normally' goes by grabbing its 'action' attribute:
'Sign Up': ->
post_url = $('#form').attr 'action' #=> users/sign_up (for example)
However, if I have this in my routes file:
post 'users/sign_up', to: 'users#create', as: :user_create
I'm wondering why I can't just do this:
'Sign Up': ->
post_url = <%= user_create_path %>
While erb tags are supported, I get this error when using a named route:
undefined local variable or method 'user_create_path'
Just wondering why this is happening... I mean using the named route helpers could be excellent. Although it may be less confusing to grab it from the form. Still think rails / coffeescript gem should implement this functionality.
The named routes helpers are probably not being included outside a controller/view.
You can access them in Rails.application.routes.url_helpers.user_create_path.
You could also use a gem like js-routes to do the job for you.

Rails undefined method in Controller

I have a Link resource (Link as in url's). I have a method in my Links controller to determine whether or not a link that a user enters has the "http://" prefix, and if not, to append that prefix to the URL. Although I defined the method in my Links controller, I am getting an undefined method error.
Here is the relevant portion of my links controller:
helper_method :link_formatter
def link_formatter(url)
prefix = "http://"
url.include?(prefix)? url : prefix + url
end
Here is my links view:
<%= link_to link.description, link_formatter(link.url), :target => '_blank' %>
The error:
undefined method `link_formatter' for #<#<Class:0x007fbd51e2ea08>:0x007fbd5250cfa0>
The link_formatter is a generic function that can be used by any view in your Rails application. To make it accessible by all your views add it to the ApplicationHelper as #Damien Roche said. The application helper is located in app/helpers/application_helper.rb. You can also generate a new helper by running rails generate helper HELPER_NAME on the command line.

add get parameter to helper methods

I added the following line in config/routes.rb file :
get '/movies/similar', :contoller => 'movies', :action => 'similar'
I see that there is a helper method with name 'movies_similar_path'. I want to add a get parameter to 'movies_similar_path' with name 'director' so I can get '/movies/similar?director=someone'
How can I do it?
No need to define parameter in the helper, you can assign it in the link_to.
link_to "Similar", movies_similar_path(:director => #director.id)
noticed the typo in 'controller' in your routes line?
What you want, you do in the form of page you are calling by adding a hidden field, or putting the extra fields in the 'link_to'. Like "link_to "simsforthisdirector", movies_similar_path(#movie), :director=>#director.name
See: http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to
Especially the examples will help you out.
If you are using rails 3 then you can declare route as -
get '/movies/similar(/:director)', :to => 'movies#similar'
and while using you can use it as -
movies_similar_path
or
movies_similar_path(#director.name)

Resources