Note that there are some explanatory texts on larger screens.

plurals
  1. POin Rails : Retrieve user input from a form that isn't associated with a Model, use the result within the controller
    text
    copied!<p>Here's a simplified version of what I'm trying to do :</p> <ol> <li>Before any other actions are performed, present the user with a form to retrieve a string.</li> <li>Input the string, and then redirect to the default controller action (e.g. index). The string only needs to exist, no other validations are necessary.</li> <li>The string must be available (as an instance variable?) to all the actions in this controller.</li> </ol> <p>I'm very new with Rails, but this doesn't seem like it ought to be exceedingly hard, so I'm feeling kind of dumb.</p> <p>What I've tried : I have a <code>before_filter</code> redirecting to a private method that looks like</p> <pre><code>def check_string if @string return true else get_string end end </code></pre> <p>the <code>get_string</code> method looks like </p> <pre><code>def get_string if params[:string] respond_to do |format| format.html {redirect_to(accounts_url)} # authenticate.html.erb end end respond_to do |format| format.html {render :action =&gt;"get_string"} # get_string.html.erb end end </code></pre> <p>This fails because i have two render or redirect calls in the same action. I can take out that first <code>respond_to</code>, of course, but what happens is that the controller gets trapped in the <code>get_string</code> method. I can more or less see why that's happening, but I don't know how to fix it and break out. I need to be able to show one form (View), get and then do something with the input string, and then proceed as normal.</p> <p>The <code>get_string.html.erb</code> file looks like </p> <pre><code>&lt;h1&gt;Enter a string&lt;/h1&gt; &lt;% form_tag('/accounts/get_string') do %&gt; &lt;%= password_field_tag(:string, params[:string])%&gt; &lt;%= submit_tag('Ok')%&gt; &lt;% end %&gt; </code></pre> <p>I'll be thankful for any help!</p> <h2>EDIT</h2> <p>Thanks for the replies...<br> @Laurie Young : You are right, I was misunderstanding. For some reason I had it in my head that the instance of any given controller invoked by a user would persist throughout their session, and that some of the Rails magic was in tracking objects associated with each user session. I can see why that doesn't make a whole lot of sense in retrospect, and why my attempt to use an instance variable (which I'd thought would persist) won't work. Thanks to you as well :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload