Trouble implementing inline rjs

So I’m trying to re-factor some controller code with over 20 rjs
templates (that all do the same thing, but for different methods). My
original code is pretty straightforward. I just create an object then
re-render a couple partials.

Controller:

def input_event
@person = Person.find(params[:id])
event = Event.new(params[:event])
if event.save
return if request.xhr?
render :partial => ‘events’
render :partial => ‘inputs’
end
end

input_event.rjs

page.replace_html(“event_list”, :partial => “events”, :object =>
@person)
page.replace_html(“input_list”, :partial => “inputs”, :object =>
@person)

After browsing around, there appear to be two methods of re-factoring to
inline rjs. The first is to throw the contents of the rjs file directly
into the controller method like this:

def input_event
@person = Person.find(params[:id])
event = Event.new(params[:event])
if event.save
return if request.xhr?
render :update do |page|
page.replace_html(“event_list”, :partial => “events”, :object =>
@person)
page.replace_html(“input_list”, :partial => “inputs”, :object =>
@person)
end
end
end

The second method is to create a method in the corresponding helper
module to handle the rjs. This would be more attractive since I could
use this same method for my 20+ controller methods.

Controller

def input_event
@person = Person.find(params[:id])
event = Event.new(params[:event])
if event.save
return if request.xhr?
render(:update){|page| page.input(@person)}
end
end

Controller Helper

def input(person)
page.replace_html(“event_list”, :partial => “events”, :object =>
person)
page.replace_html(“input_list”, :partial => “inputs”, :object =>
person)
end

However, I can’t get either of these inline methods to work. Is there
anything else I need to change?

Thanks,

Peter

anything else I need to change?
I saw a technique at railsconf europe last year, which (if I remember
correctly) went something along the lines of

def input
update_page do |page|
page.replace_html(“event_list”, :partial => “events”, :object
=>person)
page.replace_html(“input_list”, :partial => “inputs”, :object
=>person)
end
end

render :update do |page|
page << input
end

Fred

However, I can’t get either of these inline methods to work. Is there
anything else I need to change?

what does “can’t get … to work” exactly mean?

  • methods are not called?
  • get errors?
  • browser renders strange things?

Fred - That didn’t work as is, but I’ll dig around for what you saw at
rails conf eu. Thanks regardless.

Thorsten - Sorry for being vague. By can’t get to work, I mean the
controller method executes without any errors and creates the ‘event’
object, but the ‘events’ and ‘inputs’ partials don’t re-render like they
do when I use a separate rjs template.

Thorsten M. wrote:

could be:
on the view site there are two ways to trigger ajax calls:

use the :update =>
or not using it. if you use it (eg: :update => ‘my_id’), every ajax
response will be rendered in the tag with ‘my_id’
for inline-rjs you give the target-tag with every command like
page.replace_html(“input_list”…
to replace ‘input_list’. in this case you must not use :update

So with the commands I have:

page.replace_html(“event_list”, :partial => “events”, :object =>
@person)
page.replace_html(“input_list”, :partial => “inputs”, :object =>
@person)

I’m giving a target-tag to these page elements:

    <%= render :partial => 'events', :object => @claimant %>
    <%= render :partial => 'inputs', :object => @claimant %>

Right?

So therefore I should not use :update? What would I put in the place of
:update for these lines then?

render :update do |page|
render(:update){|page| page.input(@person)}

Peter M. wrote:

    <%= render :partial => 'events', :object => @claimant %>
    <%= render :partial => 'inputs', :object => @claimant %>

Should read:

    <%= render :partial => 'events', :object => @person %>
    <%= render :partial => 'inputs', :object => @person %>

could be:
on the view site there are two ways to trigger ajax calls:

use the :update =>
or not using it. if you use it (eg: :update => ‘my_id’), every ajax
response will be rendered in the tag with ‘my_id’
for inline-rjs you give the target-tag with every command like
page.replace_html(“input_list”…
to replace ‘input_list’. in this case you must not use :update

I’m giving a target-tag to these page elements:

    <%= render :partial => 'events', :object => @claimant %>
    <%= render :partial => 'inputs', :object => @claimant %>

Right?

right

So therefore I should not use :update? What would I put in the place of
:update for these lines then?

render :update do |page|
render(:update){|page| page.input(@person)}

sorry, i wasn’t clear. in the controller that’s ok, but in the view you
(may) have a link to the remote action like that:

link_to_remote “Delete this post”,
:url => { :action => “destroy”, :id => post.id },
:update => “posts”

and here you would omit the

:update => “posts”

Looks like removing “return if request.xhr?” took care of this one.
Strange.

Thorsten M. wrote:

sorry, i wasn’t clear. in the controller that’s ok, but in the view you
(may) have a link to the remote action like that:

link_to_remote “Delete this post”,
:url => { :action => “destroy”, :id => post.id },
:update => “posts”

and here you would omit the

:update => “posts”

I see what you’re saying now. I’m not using :update in my method call
though:

<% form_remote_tag :url => {:action => ‘input_event’, :id => @person }
do %>
(form)
<%= submit_tag ‘Enter’ %>

So I guess something else is going wrong. Thanks regardless :slight_smile:

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