Forum: Ruby on Rails Losing a parameter when i submit a form

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Max W. (Guest)
on 2007-07-29 20:12
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]])  %>
<h3>New link for "<%= @orig_article.title unless @orig_article == nil
%>"</h3>

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...
Astorian (Guest)
on 2007-07-30 00:29
(Received via mailing list)
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>
Max W. (Guest)
on 2007-07-30 02:10
Ahhh...figured it was something like that :)

That's very helpful, thanks a lot.
This topic is locked and can not be replied to.