# Quick 'link\_to\_remote' question

**URL:** <https://www.ruby-forum.com/t/quick-link-to-remote-question/58543>\
**Category:** Rails\
**Created:** [April 22, 2006, 10:39pm UTC](https://www.ruby-forum.com/t/quick-link-to-remote-question/58543 "2006-04-22T22:39:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dlg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dlg](https://www.ruby-forum.com/u/dlg)\
**Post date:** [April 22, 2006, 10:39pm UTC](https://www.ruby-forum.com/t/quick-link-to-remote-question/58543/1 "2006-04-22T22:39:21Z")

</div>

Hi all

i’m using link\_to\_remote to render different sets of text-input fields.  
i’m setting up links in a sidebar to do this

so, in my \_sidebar.rhtml i have

\<%= SidebarItem(‘catalogue’) %\>

this works…(in works.helper.rb)

def SidebarItem(name)  
link\_to\_remote “#{name}”,  
:update =\> ‘EditFields’,  
:url=\> {:action =\> name, :id =\> @work.id, :layout =\> false }  
end

and i have the following in works.controller.rb

```
 def catalogue
    @work = Work.find(params[:id])
    render :partial => 'catalogue'
 end

```

but when i try to save the @work before moving to a new set of fields,  
making this change doesn’t work how i want…

def SidebarItem(name)  
link\_to\_remote “#{name}”,  
:update =\> ‘EditFields’,  
:before =\> “document.form1.submit();”,  
:url=\> {:action =\> name, :id =\> @work.id, :layout =\> false }  
end

my form is this

 \<%=form\_tag ({:action =\> 'update\_part', :id =\> @work} , {:name =\> 'form1' })%\> 
\<%= render :partial =\> 'form' %\>
 \<%= submit\_tag 'Save Changes' %\> \<%= end\_form\_tag %\>

my question is this - how do i define the action ‘update\_part’ in  
works.controller.rb so that it just saves the current work but doesn’t  
redirect anywhere or render anything?

if i use the out-of-the-box :action =\> ‘update’, i briefly see my new  
set of fields but it’s then clobbered by the

redirect\_to :action =\> ‘show’, :id =\> @work

if i swap this with render: nothing =\> :true i briefly see the new  
fields before getting a blank screen

thanks guys

---

<div class="post-metadata">

**Author:** ![dlg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dlg](https://www.ruby-forum.com/u/dlg)\
**Post date:** [April 22, 2006, 11:24pm UTC](https://www.ruby-forum.com/t/quick-link-to-remote-question/58543/2 "2006-04-22T23:24:42Z")

</div>

Damian leGassick wrote:

> Hi all
> 
> i’m using link\_to\_remote to render different sets of text-input fields.  
> i’m setting up links in a sidebar to do this
> 
> so, in my \_sidebar.rhtml i have
> 
> \<%= SidebarItem(‘catalogue’) %\>
> 
> this works…(in works.helper.rb)
> 
> def SidebarItem(name)  
> link\_to\_remote “#{name}”,  
> :update =\> ‘EditFields’,  
> :url=\> {:action =\> name, :id =\> @work.id, :layout =\> false }  
> end
> 
> and i have the following in works.controller.rb
> 
> ```
> def catalogue
> @work = Work.find(params[:id])
> render :partial => 'catalogue'
> end
> 
> ```
> 
> but when i try to save the @work before moving to a new set of fields,  
> making this change doesn’t work how i want…
> 
> def SidebarItem(name)  
> link\_to\_remote “#{name}”,  
> :update =\> ‘EditFields’,  
> :before =\> “document.form1.submit();”,  
> :url=\> {:action =\> name, :id =\> @work.id, :layout =\> false }  
> end
> 
> my form is this
> 
> \<%=form\_tag ({:action =\> 'update\_part', :id =\> @work} , {:name =\> 'form1' })%\> 
> \<%= render :partial =\> 'form' %\>
> \<%= submit\_tag 'Save Changes' %\> \<%= end\_form\_tag %\>
> 
> my question is this - how do i define the action ‘update\_part’ in  
> works.controller.rb so that it just saves the current work but doesn’t  
> redirect anywhere or render anything?
> 
> if i use the out-of-the-box :action =\> ‘update’, i briefly see my new  
> set of fields but it’s then clobbered by the
> 
> redirect\_to :action =\> ‘show’, :id =\> @work
> 
> if i swap this with render: nothing =\> :true i briefly see the new  
> fields before getting a blank screen
> 
> thanks guys

Do you want to leave the “EditFields” container alone and only submit  
it? If so get rid of :update =\> ‘EditFields’. That means whatever is  
in “EditFields” will be replaced by whatever the server returns from the  
AJAX query. If the server returns nothing, your form is replaced with  
nothing. If link to remote has no :update then the result of the AJAX  
query is not sent to the DOM.

Do you want the “EditFields” to be replaced by a non-editable version of  
a “work”? then try this in your controller:

def update\_part  
@work = Word.find(params[:id])  
@work.update\_attributes params[:work]  
render :partial =\> ‘show\_single\_work’  
end

---

<div class="post-metadata">

**Author:** ![dlg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dlg](https://www.ruby-forum.com/u/dlg)\
**Post date:** [April 22, 2006, 11:58pm UTC](https://www.ruby-forum.com/t/quick-link-to-remote-question/58543/3 "2006-04-22T23:58:25Z")

</div>

Alex W. wrote:

> Do you want to leave the “EditFields” container alone and only submit  
> it? If so get rid of :update =\> ‘EditFields’. That means whatever is  
> in “EditFields” will be replaced by whatever the server returns from the  
> AJAX query. If the server returns nothing, your form is replaced with  
> nothing. If link to remote has no :update then the result of the AJAX  
> query is not sent to the DOM.
> 
> Do you want the “EditFields” to be replaced by a non-editable version of  
> a “work”? then try this in your controller:
> 
> def update\_part  
> @work = Work.find(params[:id])  
> @work.update\_attributes params[:work]  
> render :partial =\> ‘show\_single\_work’  
> end

Hi Alex, i do want to replace the content of “EditFields” with a fresh  
set of populated-fields (using a partial) - i just want to save the  
@work in it’s current state before moving on to some new fields - so  
perhaps i do need the :update option.

doing the render: partial like above renders ‘only’ the partial - it  
does not render it inside the “EditFields”

i tried this…

def SidebarItem(name)  
@work = Work.find(params[:id])  
if @work.update\_attributes params[:work]  
link\_to\_remote “#{name}”,  
:update =\> ‘EditFields’,  
:url=\> {:action =\> name, :id =\> @work.id, :layout =\> false }  
end  
end

which populates the “EditFields” div correctly but does not save the  
@work

sorry if i’m being dim…

d

---

<div class="post-metadata">

**Author:** ![dlg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dlg](https://www.ruby-forum.com/u/dlg)\
**Post date:** [April 23, 2006, 3:21am UTC](https://www.ruby-forum.com/t/quick-link-to-remote-question/58543/4 "2006-04-23T03:21:17Z")

</div>

> i tried this…
> 
> def SidebarItem(name)  
> @work = Work.find(params[:id])  
> if @work.update\_attributes params[:work]  
> link\_to\_remote “#{name}”,  
> :update =\> ‘EditFields’,  
> :url=\> {:action =\> name, :id =\> @work.id, :layout =\> false }  
> end  
> end
> 
> which populates the “EditFields” div correctly but does not save the  
> @work
> 
> sorry if i’m being dim…
> 
> d

oops, that’s dim - scratch that

can anyone help me out? i’m going round in circles on this one

it’s the .submit linked to a render that i can’t figure. doing it like  
this…

def SidebarItem(name)  
link\_to\_remote “#{name}”,  
:update =\> ‘EditFields’,  
:before =\> “document.form1.submit();”,  
:url=\> {:action =\> name, :id =\> @work.id, :layout =\> false }  
end

the js .submit triggering the standard rails update does indeed save the  
@work, and does correctly render a partial into “EditAreas” with the  
newly updated fields. the problem is that immediately afterwards

redirect\_to :action =\> ‘show’, :id =\> @work

kicks in.

it feels like what’s happening is this

- click link
- form submitted but attributes not yet updated
- in the meantime ajax call :update =\> ‘EditFields’ renders partial
- attributes get updated and then redirect\_to :action =\> ‘show’  
clobbers it

which is why i think i need an alternative form action like  
‘update\_part’ but i don’t know how to define it.

what i need is

- click link
- form submitted, attributes updated
- no redirect or render at all
- only then, ajax call :update renders partial

it sounds simple but i’m well stumped

cheers
