# Problems with form\_remote\_tag

**URL:** <https://discuss.rubyonrails.org/t/problems-with-form-remote-tag/31541>\
**Category:** rubyonrails-talk\
**Created:** [July 8, 2008, 2:11am UTC](https://discuss.rubyonrails.org/t/problems-with-form-remote-tag/31541 "2008-07-08T02:11:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [July 8, 2008, 2:11am UTC](https://discuss.rubyonrails.org/t/problems-with-form-remote-tag/31541/1 "2008-07-08T02:11:12Z")

</div>

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 %\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.... \<% end %\> ....................................

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

def update\_item &nbsp;&nbsp;@item=Item.find(session[:id])

&nbsp;&nbsp;if @item.update\_attributes(params[:item]) &nbsp;&nbsp;&nbsp;&nbsp;session[:id]=nil &nbsp;&nbsp;&nbsp;&nbsp;redirect\_to :action =\> 'index', :id =\> @item.id &nbsp;&nbsp;&nbsp;&nbsp;flash[:notice] = "#{@item.name} was successfully updated." &nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;redirect\_to :action=\>'index', :id=\> @item.id &nbsp;&nbsp;&nbsp;&nbsp;flash[:notice] = "#{@item.name} was NOT successfully updated." &nbsp;&nbsp;end end ....................................

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [July 8, 2008, 8:37am UTC](https://discuss.rubyonrails.org/t/problems-with-form-remote-tag/31541/2 "2008-07-08T08:37:42Z")

</div>

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 %\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.... \<% end %\> ....................................
> 
> .........controller................. def item @item=Item.find(params[:id]) session[:id]=@item end
> 
> def update\_item &nbsp;&nbsp;@item=Item.find(session[:id])
> 
> &nbsp;&nbsp;if @item.update\_attributes(params[:item]) &nbsp;&nbsp;&nbsp;&nbsp;session[:id]=nil &nbsp;&nbsp;&nbsp;&nbsp;redirect\_to :action =\> 'index', :id =\> @item.id &nbsp;&nbsp;&nbsp;&nbsp;flash[:notice] = "#{@item.name} was successfully updated." &nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;redirect\_to :action=\>'index', :id=\> @item.id &nbsp;&nbsp;&nbsp;&nbsp;flash[:notice] = "#{@item.name} was NOT successfully updated." &nbsp;&nbsp;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 &nbsp;&nbsp;@item=Item.find(session[:id])

&nbsp;&nbsp;respond\_to do |format| &nbsp;&nbsp;&nbsp;&nbsp;if @item.update\_attributes(params[:item]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:id]=nil &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.html { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flash[:notice] = "#{@item.name} was successfully updated." &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redirect\_to :action =\> 'index', :id =\> @item.id &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.js { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;render :text =\> "the information you want to send back to your page" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.html { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flash[:notice] = "#{@item.name} was NOT successfully updated." &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redirect\_to :action=\>'index', :id=\> @item.id &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.js { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;render :text =\> "the information you want to send back to your page" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;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](http://www.nomadsper.com)
