Ajax Update Status

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

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.

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?

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.

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