# In place ajax button

**URL:** <https://www.ruby-forum.com/t/in-place-ajax-button/89871>\
**Category:** Rails\
**Created:** [March 23, 2007, 1:09pm UTC](https://www.ruby-forum.com/t/in-place-ajax-button/89871 "2007-03-23T13:09:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cnicolaou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7bcc69/32.png) [@cnicolaou](https://www.ruby-forum.com/u/cnicolaou)\
**Post date:** [March 23, 2007, 1:09pm UTC](https://www.ruby-forum.com/t/in-place-ajax-button/89871/1 "2007-03-23T13:09:25Z")

</div>

Hello,

I am trying to create a button on the list page that triggers an action,  
like for example a list of type of wood, and the button adds dynamically  
upon clicking the number of packages linked to a certain type of wood.  
The list of wood should be updated automatically, i am able to add more  
wood packages but the list page is not updated automatically. (unless i  
refresh)  
I tried using render :partial in the function… nothing.

Any idea how to make it work properly without reloading the whole page?

CN

---

<div class="post-metadata">

**Author:** ![cnicolaou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7bcc69/32.png) [@cnicolaou](https://www.ruby-forum.com/u/cnicolaou)\
**Post date:** [March 23, 2007, 2:39pm UTC](https://www.ruby-forum.com/t/in-place-ajax-button/89871/2 "2007-03-23T14:39:48Z")

</div>

so you have a button that increments the number of packages, but the  
number  
of packages displayed does not update when the button is clicked to add  
another package?

give the element in which you write the number of packages an id, for  
example id="\<%= ‘wood’ + wood.id.to\_s %\>".

then in your controller

def incrementing\_function  
wood = Wood.find(params[:wood\_id]  
wood.packages += 1  
wood.save  
render(update) do |page|  
page.replace\_html “wood” + wood.id.to\_s, :partial =\> ‘partial you  
mentioned’, :locals =\> {:wood =\> wood}  
end  
end

if you use the partial you mentioned to render the original number of  
packages then you will replace the partial with the same partial,  
populated  
with the updated wood object.

hope that helps, if not, elaborate on your problem and maybe someone  
else  
will be able to help.

Ivor

---

<div class="post-metadata">

**Author:** ![cnicolaou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7bcc69/32.png) [@cnicolaou](https://www.ruby-forum.com/u/cnicolaou)\
**Post date:** [March 23, 2007, 3:07pm UTC](https://www.ruby-forum.com/t/in-place-ajax-button/89871/3 "2007-03-23T15:07:25Z")

</div>

Thanks Ivor,

It is a voting button for a specific candidate in the list.  
All programming process is workign fine, the thing is that i want to  
enable dynamic update of the # of votes (if possible using ajax)  
I have these lines in my candidates list:

```
<%= form_remote_tag(:url => { :action => 'vote', :id => cand.id },
                              :update => 'voting' + cand.id.to_s ) 

```

%\>

```
<div id="<%= 'voting' + cand.id.to_s %>">
<%=h cand.votes %><br/>
</div>
<%= submit_tag("Add Voter") %>
<%= end_form_tag %>

```

and the following controller function:

def vote  
cand = Candidates.find(params[:id])  
cand.votes += 1  
cand.save  
render(update) do |page|  
page.replace\_html “voting” + cand.id.to\_s, :partial =\> ‘list’,  
:locals =\> {:candidates =\> candidates}  
end  
end

The error i am getting is the following:  
“ActionController::DoubleRenderError in CandidatesController#vote  
Can only render or redirect once per action”

---

<div class="post-metadata">

**Author:** ![cnicolaou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7bcc69/32.png) [@cnicolaou](https://www.ruby-forum.com/u/cnicolaou)\
**Post date:** [March 23, 2007, 3:12pm UTC](https://www.ruby-forum.com/t/in-place-ajax-button/89871/4 "2007-03-23T15:12:11Z")

</div>

Costi Nicolaou wrote:

I also created an .RJS file for highliting:  
page.visual\_effect :highlight, “voting#{@candidates.id}”

---

<div class="post-metadata">

**Author:** ![cnicolaou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7bcc69/32.png) [@cnicolaou](https://www.ruby-forum.com/u/cnicolaou)\
**Post date:** [March 23, 2007, 3:37pm UTC](https://www.ruby-forum.com/t/in-place-ajax-button/89871/5 "2007-03-23T15:37:24Z")

</div>

Diebold??!!! nooo

Can you pls elaborate on your idea? How the current one is posting  
twice?

Thx in advance

---

<div class="post-metadata">

**Author:** ![cnicolaou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7bcc69/32.png) [@cnicolaou](https://www.ruby-forum.com/u/cnicolaou)\
**Post date:** [March 23, 2007, 3:26pm UTC](https://www.ruby-forum.com/t/in-place-ajax-button/89871/6 "2007-03-23T15:26:54Z")

</div>

Costi Nicolaou wrote:

> ```
> <%= form_remote_tag(:url => { :action => 'vote', :id => cand.id },
> :update => 'voting' + cand.id.to_s )
> 
> ```

For a vote, why not use observe\_form() and submit the vote at click  
time? Why make the user click twice just to vote once?

You don’t work for Diebold, do you?

(🙂

–  
Phlip  
[http://c2.com/cgi/wiki?ZeekLand](http://c2.com/cgi/wiki?ZeekLand) ← NOT a blog!!

---

<div class="post-metadata">

**Author:** ![cnicolaou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7bcc69/32.png) [@cnicolaou](https://www.ruby-forum.com/u/cnicolaou)\
**Post date:** [March 23, 2007, 7:39pm UTC](https://www.ruby-forum.com/t/in-place-ajax-button/89871/7 "2007-03-23T19:39:03Z")

</div>

Costi Nicolaou wrote:

> Can you pls elaborate on your idea? How the current one is posting  
> twice?

Look observe\_form() up.

Your current plan makes the user click their mouse on the check-box,  
then click on the Submit button.

If you instead observe the form, it will post each time the user  
changes it. In some usability contexts, such as voting, the post  
should be instant. Consider the Rating button on Google G. posts.  
In other usability contexts, the users should make many changes and  
send them all at once. That’s what Submit buttons are for.

–  
Phlip  
[http://c2.com/cgi/wiki?ZeekLand](http://c2.com/cgi/wiki?ZeekLand) ← NOT a blog!!
