Ruby on Rails - using helpers in html.erb - ruby-on-rails

I'm probably missing some things.
Say for example I have a helper function in app/helpers/foo_controller.rb and the code is as follows:
def sample_helper(count)
#implementaton...
end
and I want to use this helper in a webpage generated by rails and the code is as follows:
<%= sample_helper(user.id) %>
and if I try to run the webpage it will throw me an error saying that the method is not defined.
Thanks in advance!

You don't quite have the naming conventions right.
Name your helper file app/helpers/foo_helper.rb and in it you should have this:
module FooHelper
def sample_helper(count)
"#{count} items" # or whatever
end
end
And now, from any view rendered by FooController you should be able to use the sample_helper method.
Also, you should know that if you use the rails generators this structure is setup for you. All you need to do is add methods to the files that get generated. That way you don't need to guess the naming conventions.
For example, this command will make a controller file, controller test files, a helper file, and and an index view file, all ready for you to customize.
rails g controller foo index

Is your helper should be in a file called app/helpers/foo_helper.rb that contains a a module of the same name as the helper (camelized) Like:
module FooHelper
def sample_helper(cont)
# implementation
end
end
That's the way Rail auto loads helpers.

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

Where to add shared view files in Rails 5?

I am using Rails 5 and I want to use shared files in my project. I have searched the web about this, and I got some results as follow:
For Rails 3
app/views/shared
For Rails 4
app/views/application
but there was nothing for Rails 5.
I am talking about the standard; is there any specific location to add common view files in Rails 5, just as we do in other frameworks? Like in Laravel there is a layout folder?
All the people telling you that there is no "standard location" for common view files are correct. However, if you want to ask Rails where it would go looking for a partial file if you attempted to render one on the view, just take any view file you have and render a partial you know doesn't exist.
If you have, say, a UsersController with a view structure that looks something like app/view/users, and you add the following on any view there:
<%= render 'foo' %>
You'll likely get an error that says something like:
Missing partial users/_foo, application/_foo
That means that whenever you attempt to render a partial by just it's name without any path information, Rails will look for it
first under the view directory for the current controller, in this case users
then under the application directory
So, I guess then you could say that the "default location" in Rails for common view files is under the application directory.
It's possible to add to this in order to create your app's own "standard location" for common view files by overriding the ActionView::ViewPaths.local_prefixes private class method that gets mixed in to every controller in Rails:
class ApplicationController < ActionController::Base
def self.local_prefixes
['shared', controller_path]
end
private_class_method :local_prefixes
end
class UsersController < ApplicationController
def self.local_prefixes
[controller_path]
end
private_class_method :local_prefixes
end
Now, your error will say something like:
Missing partial users/_foo, shared/_foo, application/_foo
which shows that Rails will check the shared directory for partials as well before it goes looking in the application directory.
Note that this method override should happen in both the controllers, otherwise the local_prefixes in UsersController get inherited from ApplicationController and you end up with duplicated lookup paths that look like:
shared/_foo, users/_foo, shared/_foo, application/_foo
If all this is too much effort/too weird, as other people have pointed out, you can always just specify your shared partial directory manually when you render a partial:
<%= render 'shared/foo' %>
You can create new directory in app/views directory and add your shared file in that directory.
Ex:
app/views/shared # Contains all shared/partial pages
Now create your partial page in app/views/shared directory
like: app/views/shared/_my_shared_view.html.erb
Now if you want use this shared view in any view just add the below code.
<%= render :partial => 'shared/my_shared_view' %>
That's it. Hope this will help you.

Add method to a gem's helpers

I am using a gem which adds a number of View helpers under the following module hierarchy:
module Ransack
module Helpers
module FormHelpers
def search_form_for
end
end
end
end
ActionController::Base.helper Ransack::Helpers::FormHelper
I want to add my own helper method into the same module hierarchy, and make it available to all views. How do I do this?
What's the point of altering the gem with additional methods? If it's a view helper, simply create an appropriate file in the app/helpers directory of your app and add your custom helper there.
The custom helper will be available to all views.

Ruby on Rails views names dependences

May be my question it's a little weird for RoR developers, but i'm new in Ruby on Rails, and i only discover this world now - there is some dependencies in views names and definitions in controller?
If i have, for example, view called "parse-public-profile.html.erb" , should i add in controller definition with exactly this name? i mean "def parse-public-profile ... end"
I know, that this is basic, but simply i try to understand how controller knows, what views i have now; what i should change, if i will add/change-name of view, or how to define view, if in my "views" folder, i have another folder, for ex. "clients"
Thanks!
Rails follows REST this means methods as index, show, edit, update, destroy etc. are very common in an Rails controller. When you have a custom action(method) however on your controller Rails will look for the corresponding view file, so for example:
class UsersController < ApplicationController
def another_action
end
end
will try to render: app/views/users/another_action.html.erb
There is also the concept of partials which are normally called within a view file f.e. in users/index.html.erb
<% render :partial => 'form' %>
will try to render: app/views/users/_form.html.erb (note the _)
An in depth explanation can be found in the Rails guides
You can also use:
def index
render :template => "users/parse-public-profile"
end
The :template over rides the default file that Rails would have rendered.
For more info, see the Rails Guide on Layouts and Rendering at http://guides.rubyonrails.org/layouts_and_rendering.html.

Rails: Proper way to add functionality to rails methods

I'm just starting to tinker with extending the rails framework, and as an experiment, I thought I'd add some extra info inside the form_for helper. Specifically, when form_for is called, I'd like to generate an extra h1 tag such as:
# regular form_for <form> opening tag
<h1>Woohoo! It's added!</h1>
# tags fed into form_for via &proc
# form_for close <form> tag
At the moment I've added a /lib file that opens up ActiveRecord::FormHelper and overrides "form for". Needless to say writing out the whole form_for method with just the one added line added is dog ugly...but I can't call super() because, well, instead of inheriting from the method I'd like to super(), I've just overwritten it in /lib.
So, assuming I stubbornly want the functionality to be called via the same form_for tag (instead of, for example extended_form_for), what's the standard way for calling back to the original form_for method I'm overwriting? alias_method_chain? Thought I'd ask before I cement in some potentially lousy practices. If any hardened veterans could give an example I'd be appreciative.
Cheers
You could override form_for in your ApplicationHelper:
module ApplicationHelper
def form_for(*)
content_tag(:h1, "Woohoo! It's added!") + super
end
end
alias_method_chain is by far the simplest way to overwrite the method while still being able to call the original method. So in your lib file you'll want something like this:
def form_for_with_header(...)
form_for_without_header(...)
content_tag(:h1, "Header tag here")
# etc...
end

Resources