Global variable in Rails

后端 未结 4 2137
一生所求
一生所求 2021-02-14 10:58

I have a feedback form in my Rails application. The feedback form requires initializing of the @support variable, and it should be visible on every page. The initialization is v

4条回答
  •  情歌与酒
    2021-02-14 11:29

    Rather than a global variable, you probably want to put something in the ApplicationController.

    Either:

    before_filter initialize_support
    
    def initialize_support
          @support = Support.new(:id => 1)
    end
    

    Or:

    helper_method support_form
    
    def support_form
          @support_form ||= Support.new(:id => 1)
    end
    

提交回复
热议问题