views:

9

answers:

1

I'm designing a question-and-answer Ruby on Rails application.

After a user logs in, you can see a list of questions posed by other users. I have a link next to each of the questions to /answers/new?question_id=someNumber. That links to a page that displays the question (to remind the "answerer") above a standard form for submitting your answer. In order to display the question, I call @question = Question.find_by_id(params[:question_id]) and reference @question in the Haml view file:

Question details
%h2
  #{h @question.title}
#{h @question.description}
%br/
%br/
%h1
  Your answer
- form_for(@answer) do |f|
  = f.error_messages
  %p
    = f.label :description, "Enter your response here"
    %br
    = f.text_area :description
    = f.hidden_field "question", :value => @question.id
  %p
    = f.submit 'Answer'

The problem is that if I check validates_presence_of :description in Answer.rb, then I lose question_id if the user did not input anything into the description field when the page reloads, so I can't re-display the question for which the user is answering. How should I fix this?

Is there a better way of storing the question the user is trying to answer so that I can display it above the form for entering a new answer (and perhaps in other views in the future)?

If you need to see more code, please let me know.

+1  A: 

Hmm, does not hte value of @question stay the same? You know, it is member field of the controller, so as long as you do not change it there, I guess it should stay the same. If this is not true, consider storing the question id in the session.

Gabriel Ščerbák
Is there a rule of thumb for what I should store in the session and what I should not? I'm always hesitant to put things in the session because I don't know what good web dev practice calls for.
Teef L
That is another question, I personally have not enough experience with web development to give rules of thumb, but I guess if you asked you get some good answers:)
Gabriel Ščerbák