Controller member variables

I’m failing to see why a member variable in my controller won’t keep
it’s value. Here’s the setup:


def index

def mytoggle
@show_loops = !@show_loops
redirect_to :action => “index”


<% if @show_loops %> <% link_to "Hide Loops", { :action => "mytoggle" } %> <% else %> <% link_to "Show Loops", { :action => "mytoggle" } %> <% end %>


When this HTML is rendered and i click on “Hide Loops”, the
@show_loops variable does get toggled from false to true, but when it
gets redirected back to index, the variable is back to false. Am I
missing something obvious?

Rails controllers are not kept around. When a request comes in, a new
instance of the controller is made for that request only. Controller
instance variables are mainly used for flagging what data is to be
in the view. If you need data to persist across requests, you’ll need to
it in the session.


got it. thanks!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs