Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing a form instead of hard-coding a value?
    primarykey
    data
    text
    <p>I am trying to create a one-field form with a button. The field will be where the user enters his username. The value of that field will be passed into the variable <code>@username</code> in the following code:</p> <pre><code>def home require 'open-uri' @username = "brink" @url = "http://hiscore.runescape.com/index_lite.ws?player=#{@username}" end </code></pre> <p>As you can see, the <code>@username</code> variable is hard coded. I want to allow the user to input what he would like his username to be.</p> <p>After that, the user should be redirected to a page where he can see the results of his search.</p> <p>How would I implement such a feature? Not quite sure where to begin. What documentation can I look at to find help for this?</p> <p><strong>What I've looked into/Comments</strong></p> <ol> <li>I read up that forms essentially send POST requests. To my current understanding, I don't need a post request. All I need to do is take what the user specifies and substitute it into the URL</li> <li><p>Following Rich Peck's suggestion, I have created a new Search controller with a home action. Then I added <code>get "search(/:username)", :to =&gt; "search#home"</code> to my routes.</p> <p>This is great because it makes it possible to type in <strong>any</strong> username into the browser and be returned a table of information. The problem, now, is that <strong>regardless of the username I type in the browser</strong>, the username that gets passed into the crawler is still the one that is hard coded into my controller (in this case, Brink).</p> <p>What I am looking for is a way to submit a form that will cause the<code>@username</code> value to change accordingly. At the very least, we should be able to change the link up in the browser manually and achieve the same result.</p></li> <li>Removing <code>@username = "brink"</code> from my controller does not work.</li> <li><p>Here is the form I've created:</p> <pre><code>&lt;%= form_tag("/search", method: "get") do %&gt; &lt;%= label_tag(:q, "Search for:") %&gt; &lt;%= text_field_tag(:q) %&gt; &lt;%= submit_tag("Search") %&gt; &lt;% end %&gt; </code></pre> <p>When I submit it, it runs the code with the hardcoded <code>@username</code> value. This has me thinking, though. Will every form I create in this application do the same thing?</p></li> <li>I've pretty much gone blank. I feel so close, yet so far away. Haha. This is killing me! Guess I'll do some reading on routes and forms to see if it gets me closer to solving this problem. </li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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