Forum: Ruby on Rails Two forms for two models - one page

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.
6ec1e89eef5ce09fbfa56be5b6dd17b5?d=identicon&s=25 Adam Bloom (admanb)
on 2006-04-15 21:26
I've got two forms for two different models on the same page. What I
want is for one of the forms to return you to the same "new" page and
not clear the data in the other form.  I was hoping it would be as
simple as render :action => 'new' but no such luck.

Any thoughts?

Thanks,

Adam
6ec1e89eef5ce09fbfa56be5b6dd17b5?d=identicon&s=25 Adam Bloom (admanb)
on 2006-04-16 21:07
Anyone? :)

-Adam
A1c4a0bb61649f18a4d65b1c7273a50f?d=identicon&s=25 Christian Klauser (sealedsun)
on 2006-04-17 01:12
Ok, the problem is that, when you submit one form, the application does
not get the data from the other form, so it cannot store it (in the
session).

I see 2.5 possibilities:

#1: Unify the forms
#2.1: Use JavaScript to duplicate the entered text into hidden fields,
so form1 will submit the data from form1 and form2 and vice versa
#2.2: Use AJAX to periodically update the contents of the forms and save
them in the session.
Fb23bc8cd4030c526b0689276b34c8bd?d=identicon&s=25 Bryan Duxbury (bryanduxbury)
on 2006-04-17 01:46
Adam Bloom wrote:
> I've got two forms for two different models on the same page. What I
> want is for one of the forms to return you to the same "new" page and
> not clear the data in the other form.  I was hoping it would be as
> simple as render :action => 'new' but no such luck.
>
> Any thoughts?
>
> Thanks,
>
> Adam

Really, just stick the forms together. If, say, model A's "form" is not
changed, then it won't affect the database. It will be way easier than
trying to use JS/AJAX or anything like that.
6ec1e89eef5ce09fbfa56be5b6dd17b5?d=identicon&s=25 Adam Bloom (admanb)
on 2006-04-17 21:59
Ah! That makes sense. Thanks.

-Adam
6ec1e89eef5ce09fbfa56be5b6dd17b5?d=identicon&s=25 Adam Bloom (admanb)
on 2006-04-17 22:18
This works, but I can't help but feel that it should be simpler...

  def create

#this seems to be the only way to test whether a tag needs to be created
    unless (params[:tag][:name]) == ""
      @tag = Tag.new(params[:tag])
      @tag.save

#without this line the tag field will still have text in it.
      @tag = Tag.new

#and without these I get an error, because render :action doesn't run
the controller action. Right?
      @show = Show.new(params[:show])
      @show.tags = Tag.find(params[:tag_ids]) if (params[:tag_ids])
      @tags = Tag.find_all
      render :action => 'new'
    else
      @show = Show.new(params[:show])
      @show.tags = Tag.find(params[:tag_ids]) if (params[:tag_ids])
      @show.save
      @show.user = session[:user]
      if @show.save
        session[:user].shows << @show
        flash[:notice] = 'Show was successfully created.'
        redirect_to :action => 'list'
      else
        @show.destroy
        @tags = Tag.find_all
        render :action => 'new'
      end
    end
  end
6ec1e89eef5ce09fbfa56be5b6dd17b5?d=identicon&s=25 Adam Bloom (admanb)
on 2006-04-17 22:21
Oops, this is wrong:

> #and without these I get an error, because render :action doesn't run
> the controller action. Right?

Without the @tags assignment I get an error, because the page for "new"
calls @tags. Without the @show assignments I just get empty data fields.

>       @show = Show.new(params[:show])
>       @show.tags = Tag.find(params[:tag_ids]) if (params[:tag_ids])
>       @tags = Tag.find_all
>       render :action => 'new'
>     else ...

-Adam
This topic is locked and can not be replied to.