Calling method with argument in erb file Rails - ruby-on-rails

I have erb file with passing object post
<%= getTime(post) %>
And I have conroller for this erb file
helper_method :getTime
def getTime (post)
end
But controller doesn't see passed argument.

getTime(post)
should be in a helper file.
Write this in application helper and it will be accesible in your view.
def getTime (post)
end

First of all remove the space between the method and the () in the def:
def getTime(post)
It probably is seeing the argument, since if it didn't it would throw an error saying you didn't supply it.
Whatever you're doing to "verify" that the argument is "seen" is probably not accurate.

Related

Helper method with the same name as partial

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

Rails returning full object instead of integer

Rails's pluralize method was not working like I wanted (words not in english) so I set out to try my own solution. I started out simple with this method in ApplicationController:
def inflect(number, word)
if number.to_i > 1
word = word + "s"
end
return "#{number} #{word}"
end
And called it as such in my view:
<% #articles.each do |article| %>
<%= inflect(article.word_count, "word") %>
<%= inflect(article.paragraph_count, "paragraph") %>
...
<% end %>
But this got me:
undefined method `inflect' for #<#<Class:0x3ea79f8>:0x3b07498>
I found it weird that it referenced a full-fledged object when I thought it was supposed to be just an integer, so I tested it on the console:
article = Article.first
=> (object hash)
article.word_count
=> 10
article.word_count.is_a?(Integer)
=> true
So I threw in a quick words = article.word_count.to_i, but it doesn't throw a TypeError, it actually doesn't do anything, and still returns the same error: undefined method ``inflect' for #<#<Class:0x3ea79f8>:0x3b07498> in reference to the `inflect(article.word_count, "word") line.
Then I thought maybe inflect was already a Rails method and it was some sort of naming conflict, but doesn't matter what I change the method's name to, it keeps giving me the same error: undefined method ``whatever' for #<#<Class:0x3ea79f8>:0x3b07498>
I then tested it on the console and it worked fine. What's going on?
Put your inflect method in ApplicationHelper, not ApplicationController
by default all code in your helpers are mixed into the views
the view is its own entity, it is not part of the controller, when a view instance gets created (automatically when your controller action executes) it gets passed any instance variables you define in your controller action, but does not have access to controller methods directly
NOTE: you can define methods in your controller to expose them to your views by using the helper_method macro - see this post for more info on that - Controller helper_method
but in general you would define the view helper methods in the helpers classes and not in the controller

ruby on rails use variable in association call

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.

Automatically append parameters to *_url or *_path methods (Rails)

I have a particular set of views relating to one of my controllers, whereby I want any call to *_path or *_url to append a set of parameters.
Is there some magic method I can override that will let me do this? I have no idea where in the Rails code the *_path or *_url methods are even handled.
Edit for clarity: I'm looking for a way to do this such that I don't have to modify every link in every view where this needs to occur. I don't want every coder who touches this set of views to have to remember to append a parameter to every link they add to the page. The same parameter should be appended automatically. I consider a change to the *_url or *_path call a failure. Similarly, having to override every *_url or *_path call is considered a failure since a new method would have to be added/removed whenever a new link is added/removed.
You can do this by overriding url_for since all the routing methods call it.
module ApplicationHelper
def url_for(options = {})
options.reverse_merge!(#extra_url_for_options) if #extra_url_for_options
super
end
end
Now all you need to do is use a before_filter to set #extra_url_for_options to a hash to force all urls.
class MyController < ApplicationController
before_filter do { #extra_url_for_options = { :format => 'html' } }
end
Note that this will force all links to use the extra options.
Thanks to Samuel's answer, I was able to create a final working solution via a new helper, which I've included below.
module ExampleHelper
def url_for(options={})
options = case options
when String
uri = Addressable::URI.new
uri.query_values = #hash_of_additional_params
options + (options.index('?').nil? ? '?' : '&') + uri.query
when Hash
options.reverse_merge(#hash_of_additional_params)
else
options
end
super
end
end
You can try to use the with_options method. In your view you can do something like
<% with_options :my_param => "my_value" do |append| -%>
<%= append.users_path(1) %>
<% end %>
Assuming you have the users_path of course. my_param=value will be appended to the url
You could make a helper method:
def my_path(p)
"#{p}_path all the parameters I want to append"
end
and in the view use
<%= eval(my_path(whatever)) %>
Eval with give you dynamic scope, so every variable available in your view can be used in the helper. If your parameters are constant you can get rid of eval calls.

How do I call a Rails helper method from inside another helper method?

I am writing a helper that needs to call another helper, which generates html. How do I do that?
try:
include AnotherHelper
Just call it.
If it is in a different helper file, your controller can include the other helpfile by using the controller method "helper"
Added:
Here is an example:
# in the view
<%= my_helper %>
# in the helper file
def my_helper
"<div>" + someother_helper_which_generates_html + "</div>"
end
** Please add more details to your question if this isn't helping....
Something like this should help you (say, in application_helper.rb)
module ApplicationHelper
def create_div
html("this is some content")
end
def html(content)
"<div>#{content}</div>"
end
end
In this case, the create_div method is calling the html method with a string as an argument. the html method returns a string of HTML with the argument you supply embedded. in a view, it would look like:
<%= create_div %>
hope this helps!

Resources