# Sending dynamic variable from view to RJS file (AJAX)

**URL:** <https://www.ruby-forum.com/t/sending-dynamic-variable-from-view-to-rjs-file-ajax/104387>\
**Category:** Rails\
**Created:** [May 17, 2007, 1:19pm UTC](https://www.ruby-forum.com/t/sending-dynamic-variable-from-view-to-rjs-file-ajax/104387 "2007-05-17T13:19:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 17, 2007, 1:19pm UTC](https://www.ruby-forum.com/t/sending-dynamic-variable-from-view-to-rjs-file-ajax/104387/1 "2007-05-17T13:19:05Z")

</div>

How do I send a dynamic variable (album\_id) too an ajax RJS file  
copied below?

This works fine with a static span ID but Im not sure how to transfer  
the album.id

* * *

VOTE2.rjs

page.replace\_html ‘\<%= @albums.id %\>’, @albums.reload.votes\_count  
page[:vote\_score].visual\_effect :highlight

* * *

HTML (VIEW)

\<%= @albums.votes\_count %\>

```
        <div id="vote_link">

        <% if logged_in? %>

```

\<%= link\_to\_remote ‘Hype’,  
{ :url =\> { :action =\> ‘vote2’, :id =\> @albums } },  
{ :href =\> url\_for(:action =\> ‘vote2’, :id =\> @albums) } %\>\<% else %\>\<  
%= link\_to ‘Hype’,  
:controller =\> ‘account’, :action =\> ‘login’ %\>  
\<% end %\>

* * *

ALBUM CONTROLLER

def vote2  
@albums = Albums.find(params[:id])  
@albums.votes.create(:user =\> @current\_user)  
respond\_to do |wants|  
wants.html { redirect\_to :action =\> ‘index’,  
:permalink =\> @albums.permalink }  
wants.js { render }  
end

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 17, 2007, 2:52pm UTC](https://www.ruby-forum.com/t/sending-dynamic-variable-from-view-to-rjs-file-ajax/104387/2 "2007-05-17T14:52:19Z")

</div>

On 5/17/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> page.replace\_html ‘\<%= @albums.id %\>’, @albums.reload.votes\_count

.rjs files are Ruby, so lose the erb:

page.replace\_html @albums.id, @albums.reload.votes\_count

Also, in HTML, ID’s are supposed to start with a letter  
([Basic HTML data types](http://www.w3.org/TR/html4/types.html#type-id)), so I would do  
something like this:

page.replace\_html “albums\_#{@albums.id}”, @albums.reload.votes\_count

(and a similar construct in the view)
