# Problems with form\_remote\_tag

**URL:** <https://www.ruby-forum.com/t/problems-with-form-remote-tag/142452>\
**Category:** Rails\
**Created:** [July 8, 2008, 4:11am UTC](https://www.ruby-forum.com/t/problems-with-form-remote-tag/142452 "2008-07-08T04:11:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![fries88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/6bbea6/32.png) [@fries88](https://www.ruby-forum.com/u/fries88)\
**Post date:** [July 8, 2008, 4:11am UTC](https://www.ruby-forum.com/t/problems-with-form-remote-tag/142452/1 "2008-07-08T04: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 %\>  
…  
\<% end %\>  
…

…controller…  
def item  
@item=Item.find(params[:id])  
session[:id]=@item  
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  
…

---

<div class="post-metadata">

**Author:** ![fries88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/6bbea6/32.png) [@fries88](https://www.ruby-forum.com/u/fries88)\
**Post date:** [July 8, 2008, 10:37am UTC](https://www.ruby-forum.com/t/problems-with-form-remote-tag/142452/2 "2008-07-08T10: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 %\>  
> …  
> \<% end %\>  
> …
> 
> …controller…  
> def item  
> @item=Item.find(params[:id])  
> session[:id]=@item  
> 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](http://www.nomadsper.com)
