Quick 'link_to_remote' question


#1

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


#2

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


#3

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


#4

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