# Passing parameters to functions

**URL:** <https://www.ruby-forum.com/t/passing-parameters-to-functions/96682>\
**Category:** Rails\
**Created:** [June 30, 2006, 7:34pm UTC](https://www.ruby-forum.com/t/passing-parameters-to-functions/96682 "2006-06-30T19:34:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dave\_Pancost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9de0a6/32.png) [@Dave\_Pancost](https://www.ruby-forum.com/u/Dave_Pancost)\
**Post date:** [June 30, 2006, 7:34pm UTC](https://www.ruby-forum.com/t/passing-parameters-to-functions/96682/1 "2006-06-30T19:34:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Dave\_Pancost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9de0a6/32.png) [@Dave\_Pancost](https://www.ruby-forum.com/u/Dave_Pancost)\
**Post date:** [June 30, 2006, 7:58pm UTC](https://www.ruby-forum.com/t/passing-parameters-to-functions/96682/2 "2006-06-30T19:58:15Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Dave\_Pancost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9de0a6/32.png) [@Dave\_Pancost](https://www.ruby-forum.com/u/Dave_Pancost)\
**Post date:** [June 30, 2006, 8:52pm UTC](https://www.ruby-forum.com/t/passing-parameters-to-functions/96682/3 "2006-06-30T20:52:23Z")

</div>

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 🙂

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

---

<div class="post-metadata">

**Author:** ![Dave\_Pancost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9de0a6/32.png) [@Dave\_Pancost](https://www.ruby-forum.com/u/Dave_Pancost)\
**Post date:** [June 30, 2006, 9:40pm UTC](https://www.ruby-forum.com/t/passing-parameters-to-functions/96682/4 "2006-06-30T21:40:50Z")

</div>

No problem at all. Good luck!
