Passing variables across forms


#1

I’ve been reading my Agile Dev book over and over again, but I can’t
wrap my head around how to do this. There’s a question and answer
portion site of my site. You begin the process by adding a question to
the database. After that, the program should redirect you straight to
adding an answer for that question. The problem is, I don’t know how to
keep track of the answer’s question id.

Here’s what I have now:

####################### Controller
def new
@question = Question.new
end

def create
@question = Question.new(params[:question])
if @question.save
flash[:notice] = ‘Question was successfully created.’
redirect_to :action => ‘new_answer’, :id => @question
else
render :action => ‘new’
end
end

def new_answer
# the new answer must belong to a question… so I wasn’t sure
# whether to call it new answer or add answer.
@question = Question.find(params[:id])
@answer = Answer.new
@question.answers << @answer

end

def create_answer
@answer = Answer.new(params[:question_id])
if @answer.save
flash[:notice] = ‘Answer was added to the question.’
redirect_to :action => ‘show’, :id => @question
else
render :action => ‘create_answer’
end
end
###########################

########################### new_answer.rhtml
<%= start_form_tag :action => ‘create_answer’ %>
<%= render_partial ‘form_new_answer’ %>
<%= submit_tag “Create” %>
<%= end_form_tag %>
###########################

########################### _form_new_answer.rhtml

Answer to add
<%= text_area 'answer', 'answer', {:cols=>"80", :rows=>"5"} %>

###########################

########################### answer model
class Answer < ActiveRecord::Base
belongs_to :question
validates_presence_of :answer, :question_id
end
###########################

########################### question model
class Question < ActiveRecord::Base
validates_presence_of :question
has_many :answers
end
############################

Probably too much code for what you need to know…


#2

make sure that you have a hidden field in your form [:answer][:id], and
then
see the code for the controller below.

matt

else
  render :action => 'new'
end

end

def new_answer
# the new answer must belong to a question… so I wasn’t sure
# whether to call it new answer or add answer.
question = Question.find(params[:id])

    #there are a few ways to do this.  I think this is most straight

forward
# there is a sister method to answers_create(). One of them
# saves the new answer, and the other doesn’t, depnding on
# what you want

@answer = question.answers_create()

end

def create_answer

   # this or  answer.find(params[:answer][:id])  if you saved the
   # new answer in the 'new_answer' method

@answer = Answer.new(params[:answer])
if @answer.save
  flash[:notice] = 'Answer was added to the question.'
     # you don't have a 'show' method in this controller

  redirect_to :action => 'show', :id => @question

#3

How do you make a hidden field?


#4

http://api.rubyonrails.com/classes/ActionView/Helpers/FormTagHelper.html#M000408


#5

Thanks a bunch, all my searches for it were turning up the hidden text
field.


#6

Xavier L. wrote:

Thanks a bunch, all my searches for it were turning up the hidden text
field.

Oh ho ho, it just hit me…


#7

No ideas on what’s up?


#8

Thanks for the tips, but I’ve hit a bit of a wall now.
Rails crashes right after I try to add my first answer. I think it’s
passing along the values just right, because it says the parameters are

Parameters: {“answer”=>{“answer”=>“Testing 123”, “question_id”=>“10”},
“commit”=>“Create”}

The error it gives is

undefined method `stringify_keys!’ for “10”:String

####### CONTROLLER

def new_answer
# the new answer must belong to a question… so I wasn’t sure
# whether to call it new answer or add answer.
@answer = Question.find(params[:id]).answers.create
end

def create_answer
@answer = Answer.new(params[:answer][:question_id])
if @answer.save
flash[:notice] = ‘Answer was added to the question.’
redirect_to :action => ‘list’
else
redirect_to :action => ‘list’
end
end


#9

Xavier L. wrote:

Parameters: {“answer”=>{“answer”=>“Testing 123”, “question_id”=>“10”},
“commit”=>“Create”}

The error it gives is

undefined method `stringify_keys!’ for “10”:String

def create_answer
@answer = Answer.new(params[:answer][:question_id])
if @answer.save
flash[:notice] = ‘Answer was added to the question.’
redirect_to :action => ‘list’
else
redirect_to :action => ‘list’
end
end

based on the params, this line would resolve to this…

 @answer = Answer.new(10)

This is probably where your trouble begins…You probably want this…

 @answer = Answer.new(params[:answer])

_Kevin