Reputation: 217
I'm currently building a rails project that reaches out to PagerDuty to pull who is oncall and their contact information. I have a rails controller that contains a function like:
def findOncallInfo(arg1)
primary_query = {
'escalation_policy_ids' => ["MY_ID"],
'schedule_ids' => [arg1]
}
headers = {
'Content-Type' => 'application/json',
'Accept' => 'application/vnd.pagerduty+json;version=2',
'Authorization' => "Token token=keklolr0flc0pt3r"
}
ocresponse = HTTParty.get(
'https://api.pagerduty.com/oncalls',
:query => primary_query,
:headers => headers
)
rawtext = ocresponse.body
text = JSON.parse(rawtext)
policies = text["oncalls"]
policies.each do |policy|
oncall = policy["user"]
oncall.each do |key, value|
if key == 'id'
@oncallID = value
elsif key == 'summary'
@oncallName = value
end
end
end
inforesponse = HTTParty.get(
"https://api.pagerduty.com/users/#{@oncallID}/contact_methods",
headers: {
'Content-Type' => 'application/json',
'Accept' => 'application/vnd.pagerduty+json;version=2',
'Authorization' => "Token token=keklolr0flc0pt3r"
}
)
rawtext = inforesponse.body
text = JSON.parse(rawtext)
methods = text["contact_methods"]
methods.each do |method|
if method['type'] == 'email_contact_method'
@oncallEmail = method['address']
elsif method['type'] == 'phone_contact_method'
@oncallPhone = method['address']
end
end
@oncallfinal = [@oncallName, @oncallPhone, @oncallEmail]
end
Now, when I call this from my view using <%= findOncallInfo('MY_SCHEDULE') %>
it will return the last line of the function prior to [end]. For example, pulling up the page would display:
["My Name", "0000000000", "[email protected]"]
Now, if I add @oncallEmail
after @oncallfinal
(or instead of @oncallfinal
), it would display just the email address.
What I'm trying to do is call all of the variables in different places on the page, but when I do so, all I receive is 'nil' on the page. For example:
<h1>Test</h1>
<table>
<%= findOncallInfo('MY_SCHEDULE_ID') %> #displayed only for testing purposes
<tr>
<td><%= @oncallName %></td>
<td><%= @oncallPhone %></td>
<td><%= @oncallEmail %></td>
</tr>
</table>
Results in:
Test
["My Name", "0000000000", "[email protected]"]
nil nil nil
Could someone give some insight into where I might be going wrong with getting these to display?
Upvotes: 0
Views: 114
Reputation: 859
As far as I know, instance variables in the controller action are available to the the view. You are calling a function from the view, which is something different.
You have two choices:
1) Call the function from the controller action that renders the page. That would setup the variables, but the page will not render until you got the response from the remote server.
2) Re-write the view code.
The line <%= findOncallInfo('MY_SCHEDULE_ID') %>
prints out what is returned from the function, which is an array. You can replace that line with <% oncallName, oncallPhone, oncallEmail = findOncallInfo('MY_SCHEDULE_ID') %>
and then use the variables in the rest of your view.
Ideally, the function should be placed in a view helper not the controller.
Upvotes: 1
Reputation: 6121
You can't have arguments in controller and/or action in rails. If you wants to pass some argument/variable to your action, you can define them in your routes like..
get 'findOncallInfo/:arg1' => 'controller_name#findOncallInfo', as: 'find_on_call_info'
then from your view, you can call it using link_to
or similar..
However, in your case, you just need a method
which returns something based on argument, thus you can move your whole method to a helper say application_helper.rb
and then you can get the results.
Upvotes: 1