# Passing variables across forms

**URL:** <https://www.ruby-forum.com/t/passing-variables-across-forms/52868>\
**Category:** Rails\
**Created:** [January 19, 2006, 9:17pm UTC](https://www.ruby-forum.com/t/passing-variables-across-forms/52868 "2006-01-19T21:17:40Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![derdewey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@derdewey](https://www.ruby-forum.com/u/derdewey)\
**Post date:** [January 19, 2006, 9:17pm UTC](https://www.ruby-forum.com/t/passing-variables-across-forms/52868/1 "2006-01-19T21:17:40Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![derdewey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@derdewey](https://www.ruby-forum.com/u/derdewey)\
**Post date:** [January 19, 2006, 9:52pm UTC](https://www.ruby-forum.com/t/passing-variables-across-forms/52868/2 "2006-01-19T21:52:56Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![derdewey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@derdewey](https://www.ruby-forum.com/u/derdewey)\
**Post date:** [January 20, 2006, 1:03am UTC](https://www.ruby-forum.com/t/passing-variables-across-forms/52868/3 "2006-01-20T01:03:36Z")

</div>

How do you make a hidden field?

---

<div class="post-metadata">

**Author:** ![derdewey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@derdewey](https://www.ruby-forum.com/u/derdewey)\
**Post date:** [January 20, 2006, 1:39am UTC](https://www.ruby-forum.com/t/passing-variables-across-forms/52868/4 "2006-01-20T01:39:42Z")

</div>

[http://api.rubyonrails.com/classes/ActionView/Helpers/FormTagHelper.html#M000408](http://api.rubyonrails.com/classes/ActionView/Helpers/FormTagHelper.html#M000408)

---

<div class="post-metadata">

**Author:** ![derdewey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@derdewey](https://www.ruby-forum.com/u/derdewey)\
**Post date:** [January 20, 2006, 5:04am UTC](https://www.ruby-forum.com/t/passing-variables-across-forms/52868/5 "2006-01-20T05:04:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![derdewey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@derdewey](https://www.ruby-forum.com/u/derdewey)\
**Post date:** [January 20, 2006, 5:07am UTC](https://www.ruby-forum.com/t/passing-variables-across-forms/52868/6 "2006-01-20T05:07:36Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![derdewey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@derdewey](https://www.ruby-forum.com/u/derdewey)\
**Post date:** [January 20, 2006, 10:06pm UTC](https://www.ruby-forum.com/t/passing-variables-across-forms/52868/7 "2006-01-20T22:06:01Z")

</div>

No ideas on what’s up?

---

<div class="post-metadata">

**Author:** ![derdewey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@derdewey](https://www.ruby-forum.com/u/derdewey)\
**Post date:** [January 20, 2006, 6:52am UTC](https://www.ruby-forum.com/t/passing-variables-across-forms/52868/8 "2006-01-20T06:52:27Z")

</div>

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

######

---

<div class="post-metadata">

**Author:** ![derdewey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@derdewey](https://www.ruby-forum.com/u/derdewey)\
**Post date:** [January 20, 2006, 10:17pm UTC](https://www.ruby-forum.com/t/passing-variables-across-forms/52868/9 "2006-01-20T22:17:55Z")

</div>

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
