Keeping user data persistent after validates_presence_of

Posted by mathee on Stack Overflow See other posts from Stack Overflow or by mathee
Published on 2010-05-02T10:40:28Z Indexed on 2010/05/02 10:47 UTC
Read the original article Hit count: 205

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about haml