Any programmer can answer this in 2 sec, but I'm stumped


#1

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 }%>

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


#2

On 7/25/07, Gavin removed_email_address@domain.invalid 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


#3

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


#4

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


#5

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