In-Place Editor with RJS Templates

Posted by shane
on Thursday, January 19

I ran into a situation while working on a side project, where I needed to update a few elements on the page after performing an in-place edit. This can happen in a situation where what you are editing is also displayed elsewhere on the page. You don’t want to confuse the user by having the old value as well as the new value on the same page. This can be accomplished with RJS templates and some slight modifications to the Rails in-place editor macro.

NOTE: RJS templates are like RHTML templates, but for Javascript. As of this post, this feature is only available in the current Rails trunk and is not part of the 1.0 release. You need to be on EdgeRails to use them.

First, define a method in the controller that will replace the Rails in\_place\_edit_for macro. To in-place edit the ‘name’ attribute of a model named ‘Event,’ do:

1
2
3
4
5
6
7
8
  def set_event_name
    @event = Event.find(params[:id])
    previous_name = @event.name
    @event.name = params[:value]
    @event.name = previous_name unless @event.save

    # include logic needed for partials in RJS template
  end

Then create a new file in the view folder called set\_event\_name.rjs. This needs to include the partial containing the in-place editor view code, as well as anything else you need to update on the page.

1
2
3
4
page.replace_html 'in_place_edit_results', :partial => "desc"
page.replace_html 'upcoming', :partial => "upcoming"
page.visual_effect :highlight, 'upcoming', :duration => 1
page.replace_html 'month', :partial => "month"

The view, _desc.rhtml, can use the built-in Rails macro helper:

<%= in_place_editor_field :event, :name %>

Check out this great article if you need to validate and sanitize the data.

Comments

Leave a response

  1. Andrew TurnerFebruary 24, 2006 @ 08:44 AM
    What would be nice is if one of the options was a default if the actual parameter was currently empty/null, because otherwise the editable size is 0 , and the user can't click on anything to set it the first time. <%= in_place_edit_for :user, :website, :default => "http://" %>
  2. shaneFebruary 27, 2006 @ 07:10 PM
    Andrew, I believe the last time I looked at the scriptaculous source for the in place editor, it did set the default size to 1. But that was over a month ago and I don't know if the code has changed since then.
  3. Lori OlsonMarch 15, 2006 @ 09:08 AM
    Well, I thought this would solve my problems, but... I'm using Rails 1.0 with the RJS plugin. When I use the code from above, I can update another field on the page, but when the in place editor goes away, the displayed contents of the result is all the javascript generated by the .rjs file. Is this just an RJS plugin problem? If so, can you think of a work around? Our application is in production, and we don't want to go with Edge Rails if we don't absolutely have to...
  4. ShaneMarch 15, 2006 @ 07:22 PM
    Lori-- One of the lines of the RJS template must replace the contents of the div containing the editor. You must be doing this to see any difference at all on the page, so maybe something is wrong with the partial you are replacing the editor with?
  5. Lori OlsonMarch 16, 2006 @ 07:45 AM
    Well, I have it working now... mostly. I had to update to the scriptaculous 1.5.3 release javascripts, in order to get the new evalScripts option (which is required), and I had to do some monkey patching on the in_place_editor to support the new evalScripts option. Note that even Edge Rails lacks in_place_editor support for the evalScripts option AFAIKS. I'll have to find time to submit all this stuff back for inclusion in the RJS plugin for Rails 1.0.
Comment