Problems with form_remote_tag

Hi,

I am trying to use form_remote_tag to update a record and then update
the section of the page where id=‘opertion_panel’. There is a javascript
validation that validates the input before the record is being updated.
This validation works and the record was successfully updated. The
problem is after the record is being updated, it didn’t update
operation_panel. Instead it redirects to another page. Where did it went
wrong? What should I do?

Please help.

Thanks.

…view…

<% form_remote_tag(:url => {:action => :update_item}, :update =>
“operation_panel”, :before=>“return validateForm(this); return false;”)
do %>

<% end %>

…controller…
def item
@item=Item.find(params[:id])
session[:id][email protected]
end

def update_item
@item=Item.find(session[:id])

if @item.update_attributes(params[:item])
session[:id]=nil
redirect_to :action => ‘index’, :id => @item.id
flash[:notice] = “#{@item.name} was successfully updated.”
else
redirect_to :action=>‘index’, :id=> @item.id
flash[:notice] = “#{@item.name} was NOT successfully updated.”
end
end

user splash wrote:

Hi,

I am trying to use form_remote_tag to update a record and then update
the section of the page where id=‘opertion_panel’. There is a javascript
validation that validates the input before the record is being updated.
This validation works and the record was successfully updated. The
problem is after the record is being updated, it didn’t update
operation_panel. Instead it redirects to another page. Where did it went
wrong? What should I do?

Please help.

Thanks.

…view…

<% form_remote_tag(:url => {:action => :update_item}, :update =>
“operation_panel”, :before=>“return validateForm(this); return false;”)
do %>

<% end %>

…controller…
def item
@item=Item.find(params[:id])
session[:id][email protected]
end

def update_item
@item=Item.find(session[:id])

if @item.update_attributes(params[:item])
session[:id]=nil
redirect_to :action => ‘index’, :id => @item.id
flash[:notice] = “#{@item.name} was successfully updated.”
else
redirect_to :action=>‘index’, :id=> @item.id
flash[:notice] = “#{@item.name} was NOT successfully updated.”
end
end

In your update_item method, you do a redirection (redirect_to) on both
cases, so it’s seems logic that you get that redirection and nothing get
updated in your page.

To handle both normal and ajax requests try to do something like this:

def update_item
@item=Item.find(session[:id])

respond_to do |format|
if @item.update_attributes(params[:item])
session[:id]=nil
format.html {
flash[:notice] = “#{@item.name} was successfully updated.”
redirect_to :action => ‘index’, :id => @item.id
}
format.js {
render :text => “the information you want to send back to your
page”
}
else
format.html {
flash[:notice] = “#{@item.name} was NOT successfully updated.”
redirect_to :action=>‘index’, :id=> @item.id
}
format.js {
render :text => “the information you want to send back to your
page”
}
end
end
end

The content of format.html will be executed when the request comes from
a regular post (if javascript is turned off by your client browser) and
format.js for ajax call.

Hope this helps.

Guillaume
http://www.nomadsper.com

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs