Forum: Ruby on Rails Sending dynamic variable from view to RJS file (AJAX)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Abed21abff74d51f99d8ae6ad8e5a3e9?d=identicon&s=25 liftedmedia@gmail.com (Guest)
on 2007-05-17 13:19
(Received via mailing list)
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)

<td>
<span id="<%= albums.id %>"><%= @albums.votes_count %></span>
</td>
          </tr>
          <tr>
           <td id="rankhype">

            <div id="vote_link">

            <% if logged_in? %>
<a href="#" id="hype"><%= 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
813f535246722b7bf02aacc9ce818de8?d=identicon&s=25 Bob Showalter (Guest)
on 2007-05-17 14:52
(Received via mailing list)
On 5/17/07, liftedmedia@gmail.com <liftedmedia@gmail.com> 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
(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)
This topic is locked and can not be replied to.