0

I have a report object that get's data from other APIs when the user presses the "Get Report" Button.

How do I design new and create to get parameters from another function? This is what my controller looks like:

def new
  @run = Run.new
end

def create
  param_holder = get_payload
  @run = Run.new(param_holder)
end

def get_payload
  statushash = Hash.new

  # placeholder data for testing below 
  statushash[:daterun] = Time.now.strftime("%m/%d/%Y")
  statushash[:status] = "OK"
  statushash[:count] = 13

  return statushash
end

Does anyone have suggestions for learning resources and to ask basic questions like this?

sawa
  • 156,411
  • 36
  • 254
  • 350
RedOster
  • 285
  • 2
  • 12
  • trying to call controller methods manually is something that beginners often get caught up on. It's better to move the logic to some shared place and then call it from the controller action and elsewhere if needed. See http://stackoverflow.com/questions/15260984/guidelines-for-where-to-put-classes-in-rails-apps-that-dont-fit-anywhere – max pleaner May 16 '17 at 19:41
  • Are you trying to access `params` from an earlier request, if so, you can't, `params` is reset for every request. If so, why would you want to do this? – Kris May 16 '17 at 19:41

1 Answers1

0

I find it best to abstract out all API call logic to its own object which I call a service object. This object has methods with your baseline API calls which are returning the JSON. You can then pass these hashes (JSON) to your RUN model or whatever model you're using to populate the report data with and extract what you need from them with methods, or just pass the entire hash in if it has all the required attributes.

The way I would do this is make your "Get Report" button a post that hits the create action in this provided controller.

I would not use a "new" action in this case, because that is generally reserved for some kind of user input view.