# Ajax Update Status

**URL:** <https://www.ruby-forum.com/t/ajax-update-status/182981>\
**Category:** Rails\
**Created:** [February 5, 2010, 3:56pm UTC](https://www.ruby-forum.com/t/ajax-update-status/182981 "2010-02-05T15:56:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Volkan\_SSSSSALISSS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/cab0a1/32.png) [@Volkan\_SSSSSALISSS\_S](https://www.ruby-forum.com/u/Volkan_SSSSSALISSS_S)\
**Post date:** [February 5, 2010, 3:56pm UTC](https://www.ruby-forum.com/t/ajax-update-status/182981/1 "2010-02-05T15:56:50Z")

</div>

Hello All, I am new to this Ajax update with form\_remote\_tag.

I have a model so a table with order\_line\_items which has a  
order\_line\_item\_status\_code\_id field.

I am listing the pending items just fine in a list view with a  
dropdown select tag. I want to make update to Shipped with :onchange  
from select field. Here is my code, it is not updating right now but  
actually I don’t know if anything is true here regarding the  
form\_remote\_tag.

Any help would be great.

in list.rhtml

\<% for sale\_item in @pending\_sale\_items %\>

\<% form\_remote\_tag( :url =\> { :controller =\>‘customer/  
orders’, :action =\> ‘change\_sale\_item\_status’, :id =\> sale\_item.id } )  
do %\>  
\<%= select :sale\_item, :status\_id, @statuscodes.map{ |u|  
[u.name,u.id] },  
{ },  
{ :onchange =\> “(‘change\_sale\_item\_status’).submit()”, :class =\>  
‘status smallselect’ } %\>  
\<% end %\>

\<% end %\>

in customer/orders\_controller.rb

def change\_sale\_item\_status

sale\_item = OrderLineItem.find(params[:id])  
sale\_item.order\_line\_item\_status\_code\_id = params[:sale\_item]  
[:status\_id]  
sale\_item.save  
logger.info “Product added…success”

# Refresh the page

redirect\_to :action =\> “index”

end

---

<div class="post-metadata">

**Author:** ![Volkan\_SSSSSALISSS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/cab0a1/32.png) [@Volkan\_SSSSSALISSS\_S](https://www.ruby-forum.com/u/Volkan_SSSSSALISSS_S)\
**Post date:** [February 5, 2010, 5:06pm UTC](https://www.ruby-forum.com/t/ajax-update-status/182981/2 "2010-02-05T17:06:41Z")

</div>

If the from only contains one field, then I suggest you forget about the  
“form\_remote\_tag” and just use a simple select field with an onchange  
attribute.

\<%= select\_tag(:sale\_item,  
options\_from\_collection\_for\_select(@statuscodes, :id, :name), :onchange  
=\> remote\_function(:update =\> ‘repair\_shop\_selection’, :url =\>  
{:controller =\>‘customer/orders’, :action =\> ‘change\_sale\_item\_status’},  
:with =\> “‘brand\_id=’ + this.value”) ) %\>

(btw, this will submit params[:status\_id], not  
params[:sale\_item][:status\_id]

I reckon you’d like to refresh something on the page after the request  
is completed as well, so then redirect\_to :action =\> “index” wont do.

Put

 in the view and then put the below in  
the controller in place of “redirect\_to”

render :update do |page|  
page.replace\_html(‘notification’, :text =\> “Updated”)  
end

Its probably not what you actually want to happen after the request, but  
it should get you started.

---

<div class="post-metadata">

**Author:** ![Volkan\_SSSSSALISSS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/cab0a1/32.png) [@Volkan\_SSSSSALISSS\_S](https://www.ruby-forum.com/u/Volkan_SSSSSALISSS_S)\
**Post date:** [February 5, 2010, 8:44pm UTC](https://www.ruby-forum.com/t/ajax-update-status/182981/3 "2010-02-05T20:44:48Z")

</div>

Hi thank you so much for the help.

I made it work but without the id params which I need to find the  
OrderLineItem. How can I pass the id param to the function. In view it  
is sale\_item.id. Should I use form\_remote\_tag again for this param  
because I need to pass :id and :status\_id both.

Also about rendering new objects, in my view, the table view would  
rearrange after selection, I am listing shipped items on other table.  
Is it possible? Or Should I figure out how to refresh the page?

---

<div class="post-metadata">

**Author:** ![Volkan\_SSSSSALISSS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/cab0a1/32.png) [@Volkan\_SSSSSALISSS\_S](https://www.ruby-forum.com/u/Volkan_SSSSSALISSS_S)\
**Post date:** [February 5, 2010, 8:56pm UTC](https://www.ruby-forum.com/t/ajax-update-status/182981/4 "2010-02-05T20:56:02Z")

</div>

> I made it work but without the id params which I need to find the  
> OrderLineItem.  
> My bad, forgot to include that parameter.  
> \<%= select\_tag(:sale\_item,  
> options\_from\_collection\_for\_select(@statuscodes, :id, :name), :onchange  
> =\> remote\_function(:update =\> ‘repair\_shop\_selection’, :url =\>  
> {:controller =\>‘customer/orders’, :action =\> ‘change\_sale\_item\_status’,  
> :id =\> sale\_item.id},  
> :with =\> “‘brand\_id=’ + this.value”) ) %\>

> Also about rendering new objects, in my view, the table view would  
> rearrange after selection, I am listing shipped items on other table.  
> Is it possible? Or Should I figure out how to refresh the page?  
> I dont know how much content is on the page, and how much of it has to  
> be updated. If most of it changes you can refresh it all, if not, just  
> replace the small bits that need updating and leave the rest alone.

---

<div class="post-metadata">

**Author:** ![Volkan\_SSSSSALISSS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/cab0a1/32.png) [@Volkan\_SSSSSALISSS\_S](https://www.ruby-forum.com/u/Volkan_SSSSSALISSS_S)\
**Post date:** [February 5, 2010, 10:01pm UTC](https://www.ruby-forum.com/t/ajax-update-status/182981/5 "2010-02-05T22:01:29Z")

</div>

Thanks so much for the help again : ) I solved it right now with  
form\_remote\_tag, I was playing with it when I was waiting your answer.  
Your code helped me alot to understand the process. Here is the code  
working.

\<% form\_remote\_tag(:url =\> { :controller =\>‘customer/orders’, :action  
=\> ‘change\_sale\_item\_status’ },  
:html =\> {:id =\> ‘change\_status’}) do %\>  
\<%= hidden\_field(:change\_status, :id, :value =\> sale\_item.id) %\>  
\<%= select :change\_status, :status\_id, @statuscodes.map{|u|  
[u.name,u.id]}, { },  
{:onchange =\> “$(‘change\_status’).submit()”}%\>  
\<% end %\>

def change\_sale\_item\_status

```
# If variations are present we get that as the ID instead...
sale_item = OrderLineItem.find(params[:change_status][:id])
sale_item.order_line_item_status_code_id = params[:change_status]

```

[:status\_id]  
sale\_item.save  
logger.info “Product added…success”  
redirect\_to :action =\> ‘list’

end
