Globally accessible variable in rails

I want a variable that is globally accessible to the whole app. The user will fill a single input field form. It will be then saved into the database and that variable will be accessible anywhere. So here is what I have done so far.

    class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
  before_action :save_page

  def save_page
    @page_name = params[:page_name ]
    test_page_name = PageName.new
    test_page_name.name = @page_name
    if test_page_name.save
      redirect_to(controller: 'template',action: 'templates')
    end
  end

end

The form is submitted to save_page method in ApplicaitonController and after being saved it is redirected. Because this method will redirect in case of successfully saved into database, it is giving me problem. How can I fix this?

I also tried like this but no luck

    class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
  before_action :page_name

  def save_page
    @page_name = params[:page_name ]
    test_page_name = PageName.new
    test_page_name.name = @page_name
    if test_page_name.save
      redirect_to(controller: 'template',action: 'templates')
    end
  end

  def page_name
    @user_page_name = @page_name
  end

end

but when I tried to print the value of @user_page_name in the view I got nothing.

Answers


class ApplicationController < ActionController::Base

# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.

protect_from_forgery with: :exception

before_action :page_name

def param_page

 @page_name=params[:page_name]

end

def save_page

 param_page

 test_page_name = PageName.new

 test_page_name.name = @page_name

 if test_page_name.save

   redirect_to(controller: 'template',action: 'templates')

 end

end

 def page_name

  param_page

  @user_page_name = @page_name

 end

end

First off:

DON'T PUT CONTROLLER ACTIONS IN THE APPLICATION CONTROLLER

Second, I assume, from the code you posted, this page name is unique to the session? Create a PageController with a new and create method:

class PageController < ApplicationController
    def new
        @page = Page.new
    end

    def create
        page = Page.new(page_params)
        if page.save
            session[:page] = page
            redirect_to :where_ever
        else
            render :new
        end
    end

    private

    def page_params
        params.require(:page).permit(:name)
    end
end

You can now access the the page name in any controller or view by calling session[:page]. In fact, you can do this without saving anything to the DB if applicable.


As I think you have to modify your method page name as:

def page_name

save_page # need to call this method for #access instance variable

@user_page_name=@page_name

end

This should be work.


Need Your Help

Troubleshooting SQL Server Strange Timeout

sql sql-server timeout

I have a fairly generic select query. When I select the top 1245 results of a particular result set, it runs in under a second, as expected. However, if I run it for 1246, it runs continuously as i...

CSS selector and XPath in Selenium Python

python-2.7 selenium xpath selenium-webdriver css-selectors

How about using CSS selector in Selenium Python if I am not getting id or name or class of that HTML element ? How about preferring CSS in comparison to XPath?