Passing parameters to functions

I’m new with ROR and am confused about passing parameters to functions.
I have a controller called Schedules and that calles a calendar helper
for a view called show_cal.rhtml. I need to be able to pass in a
specified month and year to get the correct schedule calendar to display
in the view. I’m just starting with this and am simply trying to
display the parameters I’m trying to pass in. Here’s what I’ve got so
far:

Controller:

class SchedulesController < ApplicationController

def index
show_cal(yr, mo)
render :action => ‘show_cal’
end

#…other functions not listed

def show_cal(yr, mo)

set up dates based on yr, and mo

end

end

View: show_cal.rhtml

Calendar

<%= yr %> <%= mo %>

<%= link_to ‘previous month’, :url =>

Calendar


<%= yr %>
<%= mo %>

<%= link_to ‘previous month’, :url => ‘/schedules/show_cal?yr=2006&mo=5’
%>

I know that this doesn’t work, but I’m trying to figure out how to get
mo and yr to display, and how to pass them into the show_cal function
from my link_to.

I hope this makes sense.

Any ideas?

Thanks

Dave

Welcome to Rails!

Before you get too far, you need to get yourself used to doing things
‘the
rails way’. If you haven’t done the ‘depot’ application in the Agile Web
Development book, you should really do that.

To fix your problem, we’ll use the link_to using parameters rather than
a
url.

<%= link_to ‘previous month’, :controller=>“schedules”, :action =>
“show_cal”, :yr=@previous_year, :mo =>@previous_month %>

Retrieve values using params[]

<%=params[:yr] %>
<%=params[:mo] %>

Now, in your controller, you’ll need to do some logic to determine what
the
previous and next month’s dates will be so you can make them available.

def show_cal
   @previous_month = find_previous_month params[:mo]
   @next_month = find_next_month params[:mo]
   # the rest of your code
end

Hope that helps a bit. If not, let me know. Good luck!

Hi, Brian,

Thank you, thank you, thank you, thank you. What you gave me was just
what I needed. You unlocked a mental block I just couldn’t seem to get
through. I wasn’t getting that you can pass multiple parameters and
name them what you need to name them. Most of the parameters passed in
the depot application were simple :id parameters and I just had a brain
dead moment by not putting it all together. Boy, do I feel dumb :slight_smile:

Brian H. wrote:

Welcome to Rails!

Before you get too far, you need to get yourself used to doing things
‘the
rails way’. If you haven’t done the ‘depot’ application in the Agile Web
Development book, you should really do that.

Thanks for the welcome. I really love ROR, there’s just some adjusting
I need to make to my thinking as I progress. Unfortunately, because of
circumstances beyond my control, I had to jump in to the deep end, but
for me that’s the quickest way to learn.

Anyway, thanks again for the assistance. I truly appreciate it.

Dave

No problem at all. Good luck!