Forum: Ruby on Rails Any programmer can answer this in 2 sec, but I'm stumped.

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.
D006ba456e634e2bf249b0d021000efc?d=identicon&s=25 Gavin (Guest)
on 2007-07-26 00:49
(Received via mailing list)
I've just started working with Rails. And I can't figure out what
seems to be the easiest thing imaginable.  I need to send @ferry.id
from a view in ferry_controller...

<%= link_to 'Add new schedule', {:controller => 'schedule', :action =>
'new', ferry_id => @ferry.id }%></d>

to a method in schedule_controller...

def new
    @schedule = Schedule.new
end

and have that value survive into the 'new' view where it will be
returned in Schedule.new(params[:schedule]).

How is this done?  I'm must be utterly simple but I can't figure it
out.

Gavin
42172acdf3c6046f84d644cb0b94642c?d=identicon&s=25 Pat Maddox (pergesu)
on 2007-07-26 00:55
(Received via mailing list)
On 7/25/07, Gavin <gavhug@gmail.com> wrote:
> def new
>
>
> >
>

Hey Gavin,

You can create an instance variable the same as you do with the
schedule.
@ferry_id = params[:ferry_id]

If the ferry_id is part of the schedule, then you might want to create
the Schedule object with the ferry id
@schedule = Schedule.new :ferry_id => params[:ferry_id]

In either case you'll need a hidden tag on the new page, so the param
gets passed into the create action.  If you do things the second way
you just do
<%= f.hidden_field :ferry_id %>

and it'll be slurped up in params[:schedule]

Pat
D006ba456e634e2bf249b0d021000efc?d=identicon&s=25 Gavin (Guest)
on 2007-07-26 01:59
(Received via mailing list)
Hey Pat

Thanks for the quick reply. I'm still not sure how the @ferry_id
travels from my ferry_controller's view to the schedule_controller.
Is it something like this:

<%= link_to 'Add new schedule', {:controller => 'schedule', :action =>
'new', ferry_id => @ferry.id }%>

then this:

def new(*args)
    @schedule = Schedule.new :ferry_id => params[:ferry_id]
end

Something stills feels awkward about it.

Gavin
42172acdf3c6046f84d644cb0b94642c?d=identicon&s=25 Pat Maddox (pergesu)
on 2007-07-26 02:09
(Received via mailing list)
That's precisely it, except you'll use :ferry_id in the link_to call of
course.

How does it feel awkward to you?

Pat
D006ba456e634e2bf249b0d021000efc?d=identicon&s=25 Gavin (Guest)
on 2007-07-26 02:49
(Received via mailing list)
Hey!  It works!

I came from C# and .net and it seems to me that the syntax in Ruby and
Rails is less clear.  I've only been at it for a week
though...probably just need more time to let it soak in.

Thanks for the help, Pat.

Gavin
This topic is locked and can not be replied to.