Rails helper functions - ruby-on-rails

Where can I find a complete list of Rails helper functions (such as form_for)?

Most of them are under ActionView::Helpers in documentation.
javascript helpers
form helpers
url helpers
tag helpers
asset tag helpers
date helpers
There are more available, so downloading Rails source and looking under action_view/helpers is likely your best bet.

ApiDock is a pretty good source of documentation. Check out http://apidock.com/rails/browse under ActionView::Helpers.
Also Obie Fernandez's book "The Rails Way" has a comprehensive (I believe) chapter on them.

Related

render doesn't know where to look for partials (from helper code)

When I try to render a partial from a helper, it fails with this (condensed) error message:
Missing partial /_cube_icon with [...]. Searched in:
Note that the list of searched directories is empty!
In contrast, when using render in a view, it knows where to look:
Searched in: * "/Users/Lars/GitHub/algdb/app/views"
In the helper code, I use ActionController::Base.helpers.render(). Should I use some other render function? How do I tell it where to look for partials? Could I have set up the project wrong somehow?
This is Rails 4.2.4 ยท Ruby 2.3.1
OK, I figured it out:
I was calling render from code in the helper directory, but not from a function in a standard SomethingHelper module.
When following that convention, things started working.
Partial files normally reside within the app/views directory and are not located in the helpers directory in Rails as you can see from the error message you are receiving. To solve your problem, I would move the _cube_icon file into the app/views directory and organize your code there. I recommend reading this section of the Rails documentation for views Layouts and Rendering
Additionally, it sounds like you may be new to rails so I would take a look at the conventions that rails offers. Rails, as you may or may not know, is an opinionated framework which means certain things need to go in certain locations in order for it to work. Here is another resource on just the view part of Rail's MVC framework Action View Overview. Hope this helps.
---Updated-----
If you really want to render a partial from a helper file, there are a few ways to do so but the best one to use is outlined below:
In app/helpers
module MyHelper
def custom_render
concat(render(:partial => 'cube_icon'))
end
end
Here are some links from stackoverflow to help you out.
Rendering a partial from helper #1
Rendering from a partial from herlper #2
Using concat rails

How do I override _path helpers in rails properly?

I have a model called resource in my rails app and in need of modifying the return value of the helper *resource_path*, I've read some docs and SO Q/A and they're generally suggesting put the customized helper in *app/helpers/application_helper.rb*. The thing bothers me is that what do I do with the old auto generated helper? should I do something like
undef resource_path
before I go ahead and write my own helper? Currently I have a *resource_path* method defined within ApplicationHelper, interestingly when I open rails console, app.resource_path and helper.resource_path giving me different result.
Also, I'd like to hear a deeper explanation on how *_path* helpers implemented and how they are related to *link_to* helper, as the source code are kinda hard to read with so many meta programming techniques involved
Yes, you are able to do it like following:
resources :photos, as: 'images'
in your config/routes.rb file.
More details you can find here http://guides.rubyonrails.org/routing.html especially 4.3 Overriding the Named Helpers

Where rails store standard helpers?

So where rails store its standard helpers, like form_tag - helper? i recently installed twetter-bottstrap-rails. So this gem provides some helpers that can be used in views to generate bootstrap-styled HTML. Where this helpers are stored? App/helpers - is empty - so this helpers must in the same place where standard rails helpers are.
Twitter-bootstrap-rails helpers are in twitter-bootstrap-rails gem:
https://github.com/seyhunak/twitter-bootstrap-rails/tree/master/app/helpers
Rails' form_tag is defined here: http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html
However, that won't answer your question - the helpers that twitter-bootstrap-rails provides will be in the gem itself. See: https://github.com/seyhunak/twitter-bootstrap-rails/tree/master/app/helpers

HTML Purifier equivalent for Ruby on Rails?

Has anyone encountered an equivalent to HTMLPurifier for Rails apps? Essentially I need to clean up often terribly formed HTML generated by users before saving to the DB.
http://htmlpurifier.org/
You can use the sanitize method.
sanitize(html)
There is also a Sanitize gem.
Sanitize.clean(html)
I tend to prefer the Sanitize gem because it can be used as a before_save filter in your models instead of having to use the sanitize method in each of your views.

nest helper methods in rails3

I would to nest some helper methods to simplify my application_helper, for example I have a bunch of methods dealing with currencies which apply for the entire app and I would like to put in the currencies_helper and then simply include that entire helper into the application helper.
How can I do this?
I tried:
helper :currencies
and got
undefined method `helper' for ApplicationHelper:Module
Helpers in rails are just modules which get included in controllers to help share functionality between them. There's been some weirdness around helpers in rails 3, so depending on what version you're running things may or may not work as you expect out the box.
Essentially what you want to do is add helper :all to your application_controller which will include ALL helpers in ALL your controllers. If this isn't what you want you can specify the specific helpers you want helper :currencies for example.
In the rails 3 betas helper :all was the default behavior, but I think they've reverted that in the latest release.
There's a great article that discusses how this works in rails 2, but there may be differences in rails 3, but it should be a good starting point.

Resources