List Info

Thread: How to move data from page to page




How to move data from page to page
country flaguser name
United States
2007-09-10 11:34:49

I'd like to set up a button or link on one page, that goes
to a
"creation" page but has some of the creation data
already filled out.
This is what I'm trying to do:

In an app with two objects that have a "1 to many"
relationship (lets
say owners and pets, where each pet has one owner but each
owner can
have many pets), I want to put a button or link on the
"show owner"
page that does a "create a new pet owned by this
owner".  I do NOT
want the user to have to specify the owner when they create
the pet;
it should already be done for them.

My first hope was to pass some extra data to the link_to or
button_to
call, so that some params would be set to the owner_id, and
then the
transfer to the pet/new page would happen.  But I don't see
how to do
that.

My second hope was to store the owner_id in the session, and
then have
the pet/new page look in the session and initialize the
owner field,
if that data was in there.  I do know how to do that.

Any thoughts on how to do this the RoR way?

Joshua Levy


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Ruby on Rails" group.
To post to this group, send email to rubyonrailsgooglegroups.com
To unsubscribe from this group, send email to
rubyonrails-unsubscribegooglegroups.com
For more options, visit this group at http://gro
ups.google.com/group/rubyonrails
-~----------~----~----~----~------~----~------~--~---


Re: How to move data from page to page
user name
2007-09-10 19:15:35
Hey Joshua.  

I have a pretty good idea what you're talking about, but I'm also a tad groggy at the moment.  Let's see what we can figure out.

So, let's talk about this "show owner"; page.  If we're looking at this page, we already know who the owner is.  Each owner has_many pets, right?  has_many gives us a whole pile of methods for free  http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M000642.  This includes one to initialize part of an association: build.  This is what you're after.  

Your "show owner"; page should have a link on it somewhere to an action called #new on your PetsController.  In #new, initialize your pet like this: ;

# In PetsController
def new
 owner = Owner.find(params[:id])
  pet = owner.build(params[:pet])
end

Right away we're telling pet.owner_id to equal the owner we have.  It's not saved yet, so this isn't in the database.  It's just warmed up and ready to go.

How9;s that sound?

James H.

On 9/10/07, joshualevy < joshualevyyahoo.com"> joshualevyyahoo.com> wrote:


I'd like to set up a button or link on one page, that goes to a
"creation&quot; page but has some of the creation data already filled out.
This is what I'm trying to do:

In an app with two objects that have a "1 to many" relationship (lets
say owners and pets, where each pet has one owner but each owner can
have many pets), I want to put a button or link on the "show owner";
page that does a "create a new pet owned by this owner";. &nbsp;I do NOT
want the user to have to specify the owner when they create the pet;
it should already be done for them.

My first hope was to pass some extra data to the link_to or button_to
call, so that some params would be set to the owner_id, and then the
transfer to the pet/new page would happen.&nbsp; But I don't see how to do
that.

My second hope was to store the owner_id in the session, and then have
the pet/new page look in the session and initialize the owner field,
if that data was in there.&nbsp; I do know how to do that.

Any thoughts on how to do this the RoR way?

Joshua Levy




--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails"; group.
To post to this group, send email to rubyonrailsgooglegroups.com
To unsubscribe from this group, send email to rubyonrails-unsubscribegooglegroups.com
For more options, visit this group at http://groups.google.com/group/rubyonrails
-~----------~----~----~----~------~----~------~--~---

Re: How to move data from page to page
country flaguser name
United States
2007-09-14 19:22:57

I found your reply very helpful for other problems that I
had, but I
also discovered that this would work for what I wanted:
<%= link_to 'Add Interview', :controller =>
:interview, :action =>
'new', :interviewer_id => params[:id] %>

In this case interviewer is the owner and has many
interviews, so that
line goes in the show view for interviewer.

Worked great.

Joshua


On Sep 10, 5:15 pm, "James Herdman"
<james.herd...gmail.com> wrote:
> Hey Joshua.
> I have a pretty good idea what you're talking about,
but I'm also a tad
> groggy at the moment.  Let's see what we can figure
out.
>
> So, let's talk about this "show owner" page. 
If we're looking at this page,
> we already know who the owner is.  Each owner has_many
pets, right?
>  has_many gives us a whole pile of methods for freehttp://api.rubyonrails.org/classes/Acti
veRecord/Associations/ClassMet....
>  This includes one to initialize part of an
association: build.  This is
> what you're after.
>
> Your "show owner" page should have a link on
it somewhere to an action
> called #new on your PetsController.  In #new,
initialize your pet like
> this:
>
> # In PetsController
> def new
>   owner = Owner.find(params[:id])
>   pet = owner.build(params[:pet])
> end
>
> Right away we're telling pet.owner_id to equal the
owner we have.  It's not
> saved yet, so this isn't in the database.  It's just
warmed up and ready to
> go.
>
> How's that sound?
>
> James H.
>
> On 9/10/07, joshualevy <joshual...yahoo.com> wrote:
>
>
>
> > I'd like to set up a button or link on one page,
that goes to a
> > "creation" page but has some of the
creation data already filled out.
> > This is what I'm trying to do:
>
> > In an app with two objects that have a "1 to
many" relationship (lets
> > say owners and pets, where each pet has one owner
but each owner can
> > have many pets), I want to put a button or link on
the "show owner"
> > page that does a "create a new pet owned by
this owner".  I do NOT
> > want the user to have to specify the owner when
they create the pet;
> > it should already be done for them.
>
> > My first hope was to pass some extra data to the
link_to or button_to
> > call, so that some params would be set to the
owner_id, and then the
> > transfer to the pet/new page would happen.  But I
don't see how to do
> > that.
>
> > My second hope was to store the owner_id in the
session, and then have
> > the pet/new page look in the session and
initialize the owner field,
> > if that data was in there.  I do know how to do
that.
>
> > Any thoughts on how to do this the RoR way?
>
> > Joshua Levy


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Ruby on Rails" group.
To post to this group, send email to rubyonrailsgooglegroups.com
To unsubscribe from this group, send email to
rubyonrails-unsubscribegooglegroups.com
For more options, visit this group at http://gro
ups.google.com/group/rubyonrails
-~----------~----~----~----~------~----~------~--~---


[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )