user3833034
user3833034

Reputation:

rails 4 pass object between controllers

I have a Queries Controller which handles the results of an API request and I'm trying to pass that api object to another controller without having to persist the information to the database (the point of the app is returning a list of movies available in a certain zipcode and then allowing a user to view those results and create an event with friends around that movie, so there's no need to save the movie information in my database when the api call is made since it returns a lot of movies)

Here is my create method in the Queries Controller:

def create
    @query = Query.new
    @query.zip = query_params['zip']
    @query.date = query_params['date']
    @results = data(@query.zip, @query.date)
    redirect_to results_path, :results => @results
  end

and the results method which it gets passed to

def results
end

and then the corresponding Results view where I am just trying to display the results object:

<h3>Index</h3>

<%= results %>

Upvotes: 1

Views: 1314

Answers (2)

Richard Peck
Richard Peck

Reputation: 76784

MVC

My immediate thought is your thoughts are against the MVC programming pattern (on which Rails is based):

enter image description here

Controllers are meant to take a request from HTTP, and use it to manipulate the data on your screen through a model. The model is then able to provide the data you require, which can be passed to the view.

Sending requests inter-controller is against this pattern IMO - you'll be much better manipulating the data in a single controller action, and then pulling that data from the model.

Having said that, I think you are doing things relatively well, although you may wish to consider refactoring at system-level.

--

Fix

You should be able to pass your instance variable to your results action through the params object, as described by mjhlobdell:

#app/controllers/queries_controller.rb
Class QueriesController < ApplicationController
   def query
       ...
       redirect_to results_path(results: @results)
   end
end

#app/controllers/results_controller.rb
Class ResultsController < ApplicationController
    def results
       @results = params[:results]
    end 
end

You should be able to use this to pass the data you need.

An alternative way would be to manipulate / render the response directly in the Queries create method, as here:

#app/controllers/queries_controller.rb
Class QueriesController < ApplicationController
   def create
       ...
       render :results
   end
end

Upvotes: 1

mjhlobdell
mjhlobdell

Reputation: 371

Try passing the results in the params hash to the results method

def create
  @query = Query.new
  @query.zip = query_params['zip']
  @query.date = query_params['date']
  @results = data(@query.zip, @query.date)
  redirect_to results_path(:results => @results)
end

Then:

def results
  @results = params[:results]
end

In the view:

<h3>Index</h3>

<%= @results %>

Upvotes: 0

Related Questions