# How to pass parameters with remote\_function

**URL:** <https://www.ruby-forum.com/t/how-to-pass-parameters-with-remote-function/168668>\
**Category:** Rails\
**Created:** [May 23, 2009, 9:01pm UTC](https://www.ruby-forum.com/t/how-to-pass-parameters-with-remote-function/168668 "2009-05-23T21:01:15Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![mk27](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5e9695/32.png) [@mk27](https://www.ruby-forum.com/u/mk27)\
**Post date:** [May 23, 2009, 9:01pm UTC](https://www.ruby-forum.com/t/how-to-pass-parameters-with-remote-function/168668/1 "2009-05-23T21:01:15Z")

</div>

I cannot find a complete example of this on the web.

Here’s what I’m trying to do: In a view I have a remote\_function call  
that right now looks like this:

\<% @artists.each do |artist| %\>

- { :action =\> :list\_albums }, :with =\> "id=#{artist.id}") %\>"\> \<%=artist.name%\>
\<%end%\>

and in the corresponding controller:

def list\_albums  
@albums=Album.find(params[:id])  
@albums=@albums.sort { |a,b| a.title \<=\> b.title }  
end

Either or both of these is wrong, since in the end I get a “Can’t find  
an album without id”. There is a complicating issue, too, since if I  
change the parameter name, I get “undefined local variable”.

Anyone got a quick example of the syntax for what _must_ be a very  
common and simple task???

---

<div class="post-metadata">

**Author:** ![mk27](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5e9695/32.png) [@mk27](https://www.ruby-forum.com/u/mk27)\
**Post date:** [May 23, 2009, 9:21pm UTC](https://www.ruby-forum.com/t/how-to-pass-parameters-with-remote-function/168668/2 "2009-05-23T21:21:36Z")

</div>

First off, have you ‘thrown’ the params hash that you get in the  
controller?

You can paste it back here if this did not help you solve your  
problem.

-Mike

---

<div class="post-metadata">

**Author:** ![mk27](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5e9695/32.png) [@mk27](https://www.ruby-forum.com/u/mk27)\
**Post date:** [May 23, 2009, 9:28pm UTC](https://www.ruby-forum.com/t/how-to-pass-parameters-with-remote-function/168668/3 "2009-05-23T21:28:38Z")

</div>

Mike R. wrote:

> First off, have you ‘thrown’ the params hash that you get in the  
> controller?
> 
> You can paste it back here if this did not help you solve your  
> problem.
> 
> -Mike

Where, like “out the window”? 😉 I do not understand what you mean by  
thrown.

Here’s something (else) strange (to me): I am trying to work this out  
now with link\_remote, since that’s simpler until I get the nitty gritty.

- \<%=link\_to artist.name, :action =\> "list\_albums", :find\_id =\> artist.id %\>

I notice that WEBbrick output sez:

Parameters: {“artist\_id”=\>“65”}

Shouldn’t this be passed as “find\_id”??? I guess I could have this  
backward, will test. Maybe I’ll also try to log output to a file in the  
controller, if rails will allow me to debug that way…

Anyway, really the best thing for me would be an example paralleling the  
OP request.

---

<div class="post-metadata">

**Author:** ![mk27](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5e9695/32.png) [@mk27](https://www.ruby-forum.com/u/mk27)\
**Post date:** [May 23, 2009, 9:34pm UTC](https://www.ruby-forum.com/t/how-to-pass-parameters-with-remote-function/168668/4 "2009-05-23T21:34:29Z")

</div>

Okay, I got the WEBbrick output to agree with me. Here’s the line in  
the controller now, which is still wrong:

@albums=Album.find(:all, :conditions =\> [:artist\_id =\>  
params[:find\_id]])

NoMethodError in MainController#list\_albums  
undefined method `%’ for {:artist\_id=\>“67”}:Hash

---

<div class="post-metadata">

**Author:** ![mk27](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5e9695/32.png) [@mk27](https://www.ruby-forum.com/u/mk27)\
**Post date:** [May 23, 2009, 9:38pm UTC](https://www.ruby-forum.com/t/how-to-pass-parameters-with-remote-function/168668/5 "2009-05-23T21:38:45Z")

</div>

> @albums=Album.find(:all, :conditions =\> [:artist\_id =\>  
> params[:find\_id]])

Good practice for it to read like this

@albums = Album.find(:all, :conditions =\> “where artist\_id = ‘#{params  
[:find\_id}’”)

---

<div class="post-metadata">

**Author:** ![mk27](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5e9695/32.png) [@mk27](https://www.ruby-forum.com/u/mk27)\
**Post date:** [May 23, 2009, 9:36pm UTC](https://www.ruby-forum.com/t/how-to-pass-parameters-with-remote-function/168668/6 "2009-05-23T21:36:23Z")

</div>

Well, to be honest your first post looks correct by inspection. If you  
are just posting the id though you do not necessarily need to  
use :with. You can group all the items you want within the :url block.

Also, by saying ‘throw’, I mean that in your action in the controller  
have ‘throw params’ as the first line and then when you try to access  
that action it will output, to the screen, a trace that shows you what  
parameters are coming across from the view. Very helpful. You can keep  
throwing until you get the correct params coming across and then focus  
on fixing or writing your action code.

---

<div class="post-metadata">

**Author:** ![mk27](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5e9695/32.png) [@mk27](https://www.ruby-forum.com/u/mk27)\
**Post date:** [May 23, 2009, 10:01pm UTC](https://www.ruby-forum.com/t/how-to-pass-parameters-with-remote-function/168668/7 "2009-05-23T22:01:05Z")

</div>

Hmmm, I seem to be uncovering a more basic layer to my miscomprehension.  
Thanks people, I think your input will come in handy, but bear with me  
for a minute:

Here’s where I got to in trying to debug (just noticed stdout in the  
controller is the server stdout, handy). Hence its long winded style:

[in controller method]  
all=Album.find(:all)  
@albums = Array.new  
tmp=params[:find\_id]  
all.each { |rec|  
if rec.artist\_id == tmp  
@albums.push rec  
else puts “-\>#{rec.artist\_id} != #{tmp}”  
end  
}

Now, what I find mind boggling is some of the output:

-\>67 != 67  
-\>67 != 67  
-\>67 != 67  
-\>67 != 67  
-\>67 != 67  
-\>67 != 67  
-\>67 != 67

WHAT THE HECK? I’m new to ruby, this seems an odd feature…I would  
guess this is about data types??!!? Both numbers are sourced from an  
sqlite INTEGER.

---

<div class="post-metadata">

**Author:** ![mk27](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5e9695/32.png) [@mk27](https://www.ruby-forum.com/u/mk27)\
**Post date:** [May 23, 2009, 10:24pm UTC](https://www.ruby-forum.com/t/how-to-pass-parameters-with-remote-function/168668/8 "2009-05-23T22:24:23Z")

</div>

Hassan S. wrote:

> Nope, nothing to do with Ruby – HTTP request parameters are  
> _always_ strings.

That makes sense.

Thanks much everyone!

---

<div class="post-metadata">

**Author:** ![mk27](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5e9695/32.png) [@mk27](https://www.ruby-forum.com/u/mk27)\
**Post date:** [May 23, 2009, 10:10pm UTC](https://www.ruby-forum.com/t/how-to-pass-parameters-with-remote-function/168668/9 "2009-05-23T22:10:40Z")

</div>

On Sat, May 23, 2009 at 1:01 PM, Mk 27  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> -\>67 != 67
> 
> WHAT THE HECK? I’m new to ruby, this seems an odd feature…I would  
> guess this is about data types??!!? Both numbers are sourced from an  
> sqlite INTEGER.

Nope, nothing to do with Ruby – HTTP request parameters are  
_always_ strings.

If you need to compare to an integer, use params[:whatever].to\_i

–  
Hassan S. ------------------------ removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![mk27](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5e9695/32.png) [@mk27](https://www.ruby-forum.com/u/mk27)\
**Post date:** [May 24, 2009, 12:48am UTC](https://www.ruby-forum.com/t/how-to-pass-parameters-with-remote-function/168668/10 "2009-05-24T00:48:04Z")

</div>

Furthermore, naming conventions in rails/ruby say you should not have  
a variable called tmp, temp, temporary, or any of that ambiguous  
naming.

Also, now that you know you should be casting to\_i from params, try  
using the original method I suggested

@albums = Album.find(:all, :conditions =\> “where artist\_id = ‘#{params  
[:find\_id].to\_i}’”)

and let me know if that solved the issue. It’s really bad/wasteful to  
pull down all of your artists. Also, shorthand

@collection = Model.find(:all) is the same as @collection = Model.all

likewise, you can do Model.first and Model.last to get the first and  
low entry of a table!

Good luck.

---

<div class="post-metadata">

**Author:** ![mk27](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5e9695/32.png) [@mk27](https://www.ruby-forum.com/u/mk27)\
**Post date:** [May 24, 2009, 1:26am UTC](https://www.ruby-forum.com/t/how-to-pass-parameters-with-remote-function/168668/11 "2009-05-24T01:26:15Z")

</div>

Mike R. wrote:

> and let me know if that solved the issue. It’s really bad/wasteful to  
> pull down all of your artists. Also, shorthand

Yeah, that is what I meant by “long winded”.

_Why is there this issue with the name of a local, temporary variable?_  
If the scope is only a few lines, I might as well call it “x” (which is  
why I think tmp is _unambiguous_: it indicates this variable is just  
that, and nothing more than that)…Is this just the normal “pick  
informative names” caveat, or is there more to it? Rails is slightly  
scary…

Your “where =” line did not work – I may have mistyped it, but this  
seems to do:

@albums = Album.find(:all, :conditions =\> { :artist\_id =\>  
params[:find\_id].to\_i })

Most of all: Your help is much appreciated dude, THANKS!

ps. If you have a minute, here’s a little question about what I am  
about to try: I have a list on one side with all the artists. A click  
on a list entry will put all the artist’s albums in a div box on the  
other side. Now, I want to add a menu up top with entries like “Sort  
albums by size”, but I want that to apply only to the albums that are  
currently displayed. I don’t want to produce the list again and sort it  
(since I have some more complicated plans, like allowing the user to  
build a custom list in the div box); I want to try and keep a global  
variable ($shown) and process that back in the album controller.  
Hopefully this is possible – any related tips brought to mind?

---

<div class="post-metadata">

**Author:** ![mk27](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5e9695/32.png) [@mk27](https://www.ruby-forum.com/u/mk27)\
**Post date:** [May 24, 2009, 2:06am UTC](https://www.ruby-forum.com/t/how-to-pass-parameters-with-remote-function/168668/12 "2009-05-24T02:06:18Z")

</div>

Mike R. wrote:

> > @albums=Album.find(:all, :conditions =\> [:artist\_id =\>  
> > params[:find\_id]])
> 
> Good practice for it to read like this
> 
> @albums = Album.find(:all, :conditions =\> “where artist\_id = ‘#{params  
> [:find\_id}’”)

Nope. Your “good practice” is in fact more typing and more literal SQL  
for what as far as I can see is no good reason at all. Take advantage  
of Rails’ syntactic sugar and stick with the first version.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![mk27](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5e9695/32.png) [@mk27](https://www.ruby-forum.com/u/mk27)\
**Post date:** [May 24, 2009, 7:11am UTC](https://www.ruby-forum.com/t/how-to-pass-parameters-with-remote-function/168668/13 "2009-05-24T07:11:17Z")

</div>

Note: remove the where in the string, I was typing a bit too quickly  
and that does not belong.

I should have said my preferred practice as well. But glad everything  
worked out.

On May 23, 5:06 pm, Marnen Laibow-Koser \<rails-mailing-l…@andreas-
