views:

242

answers:

1

Hi there,

another day with Rails and today I want to use Ajax. linkt_remote_link for changing a text was pretty easy so I thought it would also be easy to switch my form_for loop just to an ajax request form with remote_form_for, but the problem with the remote_form_for is that it doesn't save my changes?

Here the code that worked:

<% form_for bill, :url => {:action => 'update', :id => bill.id} do |f| %>
# make the processing e.g. displaying texfields and so on
<%= submit_tag 'speichern'%>

It produces the following html code:

<form action="/adminbill/update/58" class="edit_bill" id="edit_bill_58" method="post"><div style="margin:0;padding:0;display:inline"><input name="_method" type="hidden" value="put" /></div>
<!-- here the html things for the forms -->
<input class="button" name="commit" type="submit" value="speichern" />

Here the code which don't save me the changes and submit them:

<% remote_form_for bill, :url => {:action => 'update', :id => bill.id} do |f| %>
# make the processing e.g. displaying texfields and so on
<%= submit_tag 'speichern'%>

It produces the following html code:

<form action="/adminbill/update/58" class="edit_bill" id="edit_bill_58" method="post" onsubmit="$.ajax({data:$.param($(this).serializeArray()), dataType:'script', type:'post', url:'/adminbill/update/58'}); return false;"><div style="margin:0;padding:0;display:inline"><input name="_method" type="hidden" value="put" /></div>
<!-- here the html things for the forms -->
<input class="button" name="commit" type="submit" value="speichern" />

I don't know if I have to consider something special when using remote_form_for (see remote_form_for)

A: 

Try this

<% remote_form_for :bill, bill, :url => {:action => 'update', :id => bill.id} do |f| %>

OR

<% remote_form_for bill, bill, :url => {:action => 'update', :id => bill.id} do |f| %>

EDITED.

I think problem is with your response your 'update' method should be something like following

def update
  @bill = Bill.find(params[:id])
  if  @bill.update_attributes(params[:bill])
    render :update |page|
      page.replace_html "some_div", :partial=>'some_partial', :object=>[@bill]
      page.replace_html "notice_div", "Bill updated succesfully"
    end
  else
    render :update |page|
      page.replace_html "some_div", :partial=>'some_partial', :object=>[@bill]
      page.replace_html "notice_div", ""
    end
  end
end
Salil
Hi Salil,this doesn't work either. The second method you mentioned for the solution is the same as the first.Anway, thanks for your help.
Matthias Guenther
Correction: If I hit the reload button im my browser it's saving the request. Think I have to dig look if I have to change something in the submit_tag.
Matthias Guenther
@Matthias :- I think The problem is not with you request it's with your response.when you click on the submit your request is send to the browser also values get updated(So when you reload page you get updated values) but you not collect your response so it's not get reflected hence i need your controller code it should something similar to my edited answer.
Salil
@Salil, yeah it works right now I just had to read more about Ajax and Rails. I keep still on learning :).
Matthias Guenther
Salil
@Sali, I'm happy to have written my first Ajaxrequest and it was so simple in the end ;). The last link you mentioned is a good ressource for me to dive in deep in Ajax. Thanks for the hint.
Matthias Guenther
cheer's dude :)
Salil