# Newbie Question: Howto insert a variable into a new form

**URL:** <https://www.ruby-forum.com/t/newbie-question-howto-insert-a-variable-into-a-new-form/143170>\
**Category:** Rails\
**Created:** [July 15, 2008, 11:41pm UTC](https://www.ruby-forum.com/t/newbie-question-howto-insert-a-variable-into-a-new-form/143170 "2008-07-15T23:41:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ptremblett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dc4da7/32.png) [@ptremblett](https://www.ruby-forum.com/u/ptremblett)\
**Post date:** [July 15, 2008, 11:41pm UTC](https://www.ruby-forum.com/t/newbie-question-howto-insert-a-variable-into-a-new-form/143170/1 "2008-07-15T23:41:25Z")

</div>

I am trying to populate some fields in a new form before I display it.

I know that if I do this: @mapper = Mapper.new(:consultant\_id =\> ‘Fred’)

It works and Fred is displayed in the form but in this case  
consultant\_id is stored as a variable in sessions as it is assigned  
when the consultant logs in.  
So what I want to do is substitute “Fred” with my session variable:  
session[:consultant\_id]

If I do this: @mapper = Mapper.new(:consultant\_id =\>  
‘#{session[:consultant\_id]}’)

The code appears in the form not the name. I have tried a number of  
other ways but they all caused the app to bomb.  
Can someone please show me the correct way to do this.

Thanks,

Paul Thompson.

---

<div class="post-metadata">

**Author:** ![ptremblett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dc4da7/32.png) [@ptremblett](https://www.ruby-forum.com/u/ptremblett)\
**Post date:** [July 15, 2008, 11:46pm UTC](https://www.ruby-forum.com/t/newbie-question-howto-insert-a-variable-into-a-new-form/143170/2 "2008-07-15T23:46:06Z")

</div>

Have you tried just the following? I’ve never use Mapper, so I don’t  
know of any caveats that might apply to it specifically

@mapper = Mapper.new(:consultant\_id =\> session[:consultant\_id])

On Jul 15, 3:40 pm, “Paul Jonathan T.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![ptremblett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dc4da7/32.png) [@ptremblett](https://www.ruby-forum.com/u/ptremblett)\
**Post date:** [July 15, 2008, 11:57pm UTC](https://www.ruby-forum.com/t/newbie-question-howto-insert-a-variable-into-a-new-form/143170/3 "2008-07-15T23:57:13Z")

</div>

Thanks and yes I have tried that. It does not bomb but nothing appears  
when the form is displayed either ☹

2008/7/16 Sean C. Hess [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![ptremblett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dc4da7/32.png) [@ptremblett](https://www.ruby-forum.com/u/ptremblett)\
**Post date:** [July 15, 2008, 11:59pm UTC](https://www.ruby-forum.com/t/newbie-question-howto-insert-a-variable-into-a-new-form/143170/4 "2008-07-15T23:59:33Z")

</div>

What happens if you put the following before your code?

\<%= :consultant\_id %\> - \<%= session[:consultant\_id] %\>

Just making sure the variables are set the way you think they are.

On Tue, Jul 15, 2008 at 3:56 PM, Paul Jonathan T.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![ptremblett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dc4da7/32.png) [@ptremblett](https://www.ruby-forum.com/u/ptremblett)\
**Post date:** [July 16, 2008, 12:24am UTC](https://www.ruby-forum.com/t/newbie-question-howto-insert-a-variable-into-a-new-form/143170/5 "2008-07-16T00:24:43Z")

</div>

Sean, thanks for your help. Your first suggestion: @mapper =  
Mapper.new(:consultant\_id =\> session[:consultant\_id]) works just fine.  
What happened is that I save the data to sessions via a before filter  
in application.rb Somehow I forgot to run the filter and this came to  
light when your second suggestion failed to work.

Thanks for the help, I am not a programming noob but still very much a  
noob to rails and object orientated programming. If I were confident  
in my code, I would likely have picked up the problem earlier but not  
being sure about the code, when it failed I just zeroed in on it.

Thank you once again. It is guys like you that make the rails  
community great thing it is today.

2008/7/16 Sean C. Hess [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![ptremblett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dc4da7/32.png) [@ptremblett](https://www.ruby-forum.com/u/ptremblett)\
**Post date:** [July 16, 2008, 12:26am UTC](https://www.ruby-forum.com/t/newbie-question-howto-insert-a-variable-into-a-new-form/143170/6 "2008-07-16T00:26:28Z")

</div>

Thanks for the flattery. I’m a newbie myself, so it’s good to hear I  
could  
help somebody 😃  
~sean

On Tue, Jul 15, 2008 at 4:23 PM, Paul Jonathan T.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
