# Losing a parameter when i submit a form

**URL:** <https://www.ruby-forum.com/t/losing-a-parameter-when-i-submit-a-form/110688>\
**Category:** Rails\
**Created:** [July 29, 2007, 6:12pm UTC](https://www.ruby-forum.com/t/losing-a-parameter-when-i-submit-a-form/110688 "2007-07-29T18:12:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [July 29, 2007, 6:12pm UTC](https://www.ruby-forum.com/t/losing-a-parameter-when-i-submit-a-form/110688/1 "2007-07-29T18:12:37Z")

</div>

I have an Article item that i’m linking another article to. When the  
user hits the ‘add link’ button, i pass through a parameter called  
“original\_article\_id”, which points to the current article:

#on my article’s ‘show’ page:  
\<%= link\_to ‘Add a link’, :action =\> ‘new\_link’, :original\_article\_id =\>  
@article.id %\>

There’s no ‘new\_link’ action, just a form page called new\_link. On the  
form, i can get the original\_article\_id no problem: i get the article  
and display its title:

\<% @orig\_article = Article.find(:first, :conditions =\> [“id = ?”,  
params[:original\_article\_id]]) %\>

### New link for "\<%= @orig\_article.title unless @orig\_article == nil %\>"

So far so good. However, when the form gets submitted, it calls my  
‘create\_link’ action, in the controller, and this _can’t_ retrieve the  
original\_article\_id: doing the exact same find as above gives me nil:

#in the controller this returns nil  
@original\_article = Article.find(:first, :conditions =\> [“id = ?”,  
params[:original\_article\_id] ])

It’s as if when my form is submitted my parameter gets lost. Does  
anyone know why this is happening? Is it something to do with the  
params hash getting wiped before submitting the form details in it?

thanks in advance…

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [July 29, 2007, 10:29pm UTC](https://www.ruby-forum.com/t/losing-a-parameter-when-i-submit-a-form/110688/2 "2007-07-29T22:29:18Z")

</div>

Hi Max, I am very new to this, but I think you’d need to  
add :original\_article\_id to the params being submitted by the second  
form. The params don’t stick around. I know the following works,  
although I believe it is a beginner’s hack. To set up your add\_link  
form, run the following action. This creates a new Link object, sets  
the correct original article id, and supplies this object to your  
form.

def add\_link  
@link = Link.new  
@link.original\_article\_id = params[:original\_article\_id]  
end

in the second form, use a hidden field to hold this

\<%= hidden\_field ‘link’, ‘original\_article\_id’ %\>

when the form gets submitted, this will be added to the params hash.  
When you run the create\_link action, it will get set correctly.

def create\_link  
@link = Link.new(params[:link])  
if @link.save …

John

On Jul 29, 12:12 pm, Max W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [July 30, 2007, 12:10am UTC](https://www.ruby-forum.com/t/losing-a-parameter-when-i-submit-a-form/110688/3 "2007-07-30T00:10:44Z")

</div>

Ahhh…figured it was something like that 🙂

That’s very helpful, thanks a lot.
