ruby - Override instance variable of parent controller in child controller -


i have 3 controllers inherit applicationcontroller. child controllers are:

homecontroller

  • index

the home controller has 1 method, index, , renders form.

templatescontroller

  • templates
  • save_page_name
  • preview

the templatescontroller has 3 methods. templates lists templates , preview opens preview of selected template. save_page_name endpoint of form submitted home#index.

designscontroller

  • home
  • about

template#preview renders view iframe contains design#home.

every method in designscontroller requires value set form fetch data from. (data being fetched api.)

now. want create globally accessible variable designcontroller. i.e. want make value come form global. have put code in parent controller

here code:

class applicationcontroller < actioncontroller::base    protect_from_forgery with: :exception   before_action :page_name    def page_name     @page_name = ''   end end 

and intend on overriding in template#save_page_name:

def save_page_name   @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 

this form

<%= form_tag({controller: 'application', action: "save_page_name"}, class: "nifty_form") do%> <%= text_field_tag :page_name %> 

this template controller

class templatecontroller < applicationcontroller   def templates   end    def preview   end end 

design controller

class designcontroller < applicationcontroller   def home   @info = @page_name //should global var   render :template => "design/"+params[:template]+"/home"   end end 

if change way define page_name method in application controller to:

def page_name   @page_name ||= '' end 

the method page_name default, set @page_name empty string, allow assign different value, via page_name = "something_else"

and - @page_name not global variable. instance variable of controller class. when app calls controller, instance of controller class used, , instance variable available within context of controller instance.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -