ruby on rails use variable in association call - ruby-on-rails

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.

Related

Get controller with a path

At this time i'm trying to get the controller with a path as a parameter
Then what I need is a method that returns the controller, something like
<%= get_controller(users_path) %>
Thanks
Let me introduce you to ActionDispatch's recognize_path:
Rails.application.routes.recognize_path(users_path)[:controller]
=> "users"
Please note that if you are outside a controller or view (such as in a model or in the console, for example), you need to first include Rails.application.routes.url_helpers before passing named helpers to the method. String routes ("/users" in this case) will work in any case.

Can't call instance method from index view

I'm trying to call method (instance method) which I have defined in the controller from the index.html.erb view.
records_controller.rb:
def calc_cell_balance
4
end
index.html.erb:
<% #records.each do |r| %>
<%= r.calc_cell_balance %><br>
<% end %>
I get this error:
undefined method `calc_cell_balance' for #<Record:0x35d18d8>
I don't want to make it a class method because it's bad design.
If I put the method definition in record.rb (the model), it's working.
I'm not sure what I'm doing wrong, since it's wrong to access the model from the view, but it's the only thing working.
How can I resolve this?
Thanks.
You put an instance method in your controller, RecordsController, but you are trying to call the method on an instance of the Record class. This doesn't make sense at all. Your #records are all Record instances. You would have to do something like:
RecordsController.new.calc_cell_balance
BUT DON'T DO THAT! Your controller is there to just direct what needs to be done, and shouldn't have methods that are called outside of the controller instance itself.
Your method probably belongs in the Record model, or maybe in a helper. It is not at all wrong to access the model from the view. That's the main thing that people do. If you really wanted to not be calling any methods from the view, you could try to gather up all the information in the controller like this:
#records = Record.all
#records_calc_cell_balance = #records.collect(&:calc_cell_balance)
And then you have parallel arrays of data, but that's just silly. Calling model methods from the view is fine. Or, if you feel the method is too view-centric (like maybe you want a method to tell you what CSS class to use), put that in a view helper, which is what it's for.

Rails - How to access a Request Parameter from view in a helper

How do you access a view's request parameter inside a Helper?
My view events/index7 sets the parameter date_selected:
<div><%= link_to 'View' , events_index7_path(:date_selected => date), :class => 'btn btn-mini'%></div>
My helper is app/helpers/calendar_helper.rb
Here's a pic of it at the point in the Helper I where I want to access it.
I tried this:
classes << "dayselect" if day == DateTime.strptime(params[:date_selected], "%Y-%m-%d")
I get this error:
undefined local variable or method `params' for #<CalendarHelper::Calendar:0x007f8d215671f0>
OR should I be passing the date to the helper in a different way?
Thanks for the help!
params is a controller method, belongs to ActionController:Metal http://api.rubyonrails.org/classes/ActionController/Metal.html#method-i-params
It's possible to let View to touch params by exposing this controller method as a helper.
class FooController < ApplicationController
helper_method :params
Then, in your view you can call this helper with params as argument. my_helper(params)
But, wait, this breaks MVC principle. View should not touch params at all. All these works should be done at controller level. What if the params is incorrect? You need controller to respond that, instead of passing that responsibility to view.
So, if you need to touch it in view, it's a smell. Review the whole process, there must be a better arrangement.

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

can i underscore a url path

I want to dynamically set the url path of a
- application_path = #object.class.name.underscore + "_path"
= link_to "<input type='button' value='Cancel' class ='bigbutton go_back'/>".html_safe, application_path(#application)
but i keep getting
undefined method `application_path' for #<#<Class:0x00000103d11d80>:0x00000103d04a68>
any ideas on how to achieve this behavior
In the general case, you can use Object#send to call a method based on a symbol (which you can get from a string using to_sym):
send(application_path.to_sym, #application)
From the docs:
send(symbol [, args...]) → obj
Invokes the method identified by symbol, passing it any arguments specified.
But in this case, because your dynamic string is simple enough, Rails has a built-in method to do this, url_for:
url_for(#application)
Example from the docs:
<%= url_for(#workshop) %>
# calls #workshop.to_param which by default returns the id
# => /workshops/5
Note: this is assuming your #application is a model object where the route matches the model name. In your code there seems to be two instance variables, #object and #application, but you haven't explained them fully, so you may need to modify the above to pass in #object instead of #application.
The method url_for does exactly what you're trying to do.
url_for(#application)

Resources