random_user_0891
random_user_0891

Reputation: 2051

rails pass params or variable to multiple controller actions

I have a dropdown that displays multiple "principals", when a user picks a principal and hits submit the form passes a principal id param to the controller action.

The issue is that I need that params value in multiple controllers.

my view

  <div class="row">
    <div class="col-sm-12 button-wrapper">
      <%= form_tag(principal_analytics_path, method: 'get', class: 'form-inline justify-content-center') do %>
        <%= select_tag 'prid', options_from_collection_for_select(Principal.all, :id, :name), :prompt => "Select Principal", class: "form-control wrapper-space" %>
        <%= submit_tag "Search", class: 'btn btn-primary wrapper-space', :name => nil %>
      <% end %>
      <%= link_to "Clear", principal_analytics_path, class: "text-danger" %>
    </div>
  </div>

controller action

once I get a selected principals "books" I then need a way of passing the @pri_books variable from one controller to another. Or a way just to pass in the 'params[:prid]` to multiple controller actions. I'm not sure how to go about doing this. Maybe a function that passes the variable from one controller to another?

  # displays graphs about principals.
  def principal_analytics

    @months_back = 12

    if params[:prid].present?
      # takes a principals id as a parameter.  
      # @months_back will determine how many months are shown in the chart.
      @pri_books = Book.principal_books(params[:prid], @months_back)  
    end

    respond_to do |format|
      format.html 
      format.js {}
      format.json { render json: {success: true}}
    end    
  end

Upvotes: 0

Views: 1445

Answers (3)

SteveTurczyn
SteveTurczyn

Reputation: 36860

Use a session variable, that's what they're there for...

session[:prid] = params[:prid]

You can even set up a method in your ApplicationController to give you the principal when you need it...

class ApplicationController < ActionController::Base
  def current_principal
    principal.find_by(id: session[:prid])
  end
end

Now just use current_principal where you need it. (It'll be nil if you haven't selected one yet)

Upvotes: 3

random_user_0891
random_user_0891

Reputation: 2051

How I ended up getting it working was to make a class level variable @@class_variable and then just set that within a controller action that received the params which I could then use in other controller actions.

Not sure if this is the proper way to do this or not though but it works.

# class level variable outside of any action
 @@principals_books = nil

    # displays graphs about principals.
      def principal_analytics

        @months_back = 12

        if params[:prid].present?
          # takes a principals id as a parameter.  
          # @months_back will determine how many months are shown in the chart.
          @pri_books = Book.principal_books(params[:prid], @months_back) 

          # set the data from the local instance variable to the class variable
          @@principals_books = @pri_books 
        end

        respond_to do |format|
          format.html 
          format.js {}
          format.json { render json: {success: true}}
        end    
      end

    def another_action
      @local_variable = @@principals_books
      @local_variable.do_something
    end

Upvotes: 0

Andrew Schwartz
Andrew Schwartz

Reputation: 4657

You can use the data you get back from the controller to populate hidden form elements (hidden_field). These fields will submit their values as params/data just as any other form element to the form's action. Note: this is not secure and can be easily manipulated by users, so if that's a concern don't use this method.

Alternatively, you can store these data in Rails' session variable. This session is maintained in the server and can be used to store data across multiple requests. Be aware that to do this right you should make sure to have a sensible method to expire or delete session data to avoid (1) keeping more data around than you need and (2) confusing user experience where they expected a new blank form but had data populated from many clicks ago.

Upvotes: 0

Related Questions