Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4235290
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:28:41+00:00 2026-05-21T02:28:41+00:00

I’m learning rails, and creating a simple question/answer app to test some of the

  • 0

I’m learning rails, and creating a simple question/answer app to test some of the concepts I’ve been introduced to. I used generate scaffold to create two model classes – Question and User.

The questions table consists of a question_number (int), question_text (string) and answer_text (string).

The users table has a uid (string), name (string) and current_question (int)

I then created a controller called gateway. I have the app working now where a user can go to the main page passing their UID as a parameter (eventually will be passed by a login function) and the app will display the question they are currently working on by grabbing the question_number which matches the user’s current_question. Great.

I then created an answer method in the gateway controller. This method takes an answer the user types into a form and compares it to the answer in the database, redirecting the user to different pages depending on if the answer was right or wrong.

My issue – I set instance variables in the index method to set the current user and current question – but these do not persist to my answer method. What is the Rails best practice here? Do I:

1) Pass these as parameters to the answer method through the form, just like I pass the answer (seems inefficient when I’ve already created the instance vars in the controller, and now pass them back…)
2) Store these as vars in the session?
3) Store these in Flash?
4) Just look them up again in the answer method (don’t like repeating code, and 2 calls for same info seems wasteful)

Am I even using the right structure for this app? What would you do in this instance, where you want to grab an attribute of a user (their current authorized question) and use it several times to query another part of the database (step 1 – get question matching user’s authorized question, step 2 – check that answer user inputs is correct, step 3 – increment the user’s auth question counter)

more info, @wukerplank

No plugins for session handling. Basically, my flow goes like this:

  1. “login” user (no login logic in this little test app, so I just pass the uid as a param in the URL)
  2. The gateway controller gets the user’s current question with @question = Question.where(:question_number => @user.current_question).first
  3. User is displayed the question text and a form to enter answer
  4. Submit button calls my :answer action and passes the answer string
  5. Here is where I’d love to just call @question again, but can’t because it is not persisted. So I either have to repeat step 2, or find a place to store the data I need to reference again. I’m assuming the DRY principles of Rails would advise against repeating Step 2, so I’m wondering where/how the best way to store these (and other variables I may need) are
    Thanks!
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T02:28:42+00:00Added an answer on May 21, 2026 at 2:28 am

    Sounds like storing it in a session would be appropriate, depending on how much security you want.

    In rails3 the session is actually stored in the cookie itself and has limited size.
    If the variables are large objects then create a table in the database and just store the id in the session and look it up each time.

    Alternatively you could use memcached or equivalent to store the session and then the cookie just stores a session ID.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
We're building an app, our first using Rails 3, and we're having to build
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Seemingly simple, but I cannot find anything relevant on the web. What is the
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.