Rails in place editing plugin w/ selection

As of Rails 2.0 the in_place_editor methods have been moved into a plugin. You can download this plugin here. For some items I needed to have a dropdown selection instead of a text field. To add this functionality, I added two methods to the plugin.

Here is the full contents of ‘in_place_macros_helper.rb’ http://pastie.caboo.se/169443

Replace the contents of this file:
“your_app/vendor/plugins/in_place_editing/lib/in_place_macros_helper.rb”

Controller Usage:
in_place_edit_for :object, :method

#example_controller.rb

class MyController < ApplicationController

in_place_edit_for :user, :theme_color

end

Inserting this in the top of you controller automatically creates a method that update the object and returns the updated value as text to the browser.

View Usage:
in_place_editor_select_field :object, ‘method‘, {tag_options},{:collection => “[array]”, scriptaculous_options}

#example_view.html.erb

<label for=”user_theme_color_1_in_place_editor”>Theme Color:</label>

<%= in_place_editor_select_field :user, ‘theme_color’, {},{:collection => “[[ 1 , ‘Red’ ], [ 2 , ‘Green’ ], [ 3 , ‘Blue’ ]]”} %>

<br />

UPDATE: Get the new code for the plugin from the pasties link above
*These methods require the prototype and scriptaculous libraries so be sure to include them in your views.

Related posts

11 Responses to “Rails in place editing plugin w/ selection”

  1. Your post is very timely. I grabbed your code and am using it in one of my projects that I had punted on in_place_editing on. The formatting is a little honked in your code snippets, though. Here is my understanding of the cleaned up code:
    http://pastie.caboo.se/169298

    I get the select to show when I click on the value, but when I save it I get the RJS error where it prints the JS text to the screen.

    Did I miss something that still needs to be reformatted in the code?

    Thanks

  2. Barrett: What does your controller and view code look like, and what are you trying to get as a response. The default response is the current value of the method that is inserted back into the field that was edited.

  3. OK. I got it. Here’s the deal. In addition to this extension to the in_place_edit, I am also using a custom_in_place_editing (http://www.pluitsolutions.com/2007/03/20/custom-in_place_edit-with-validation/) s.t. the validation error messages appear in an alert.

    I also had textboxes with the in place edit barfing the RJS at me, so I was thinking that somewhere through the layers of additional customization something was off.

    Yes and No. I was leaving something out from the in_place_edit that is kind of important for “edge rails” — this originally came out with a 1.x version of rails. The :script => true bit is important. Doh!

    The other thing that was amiss is that this does not seem to work with the custom_in_place_editing, but it doesn’t need to. The validations (at least in MY model) are irrelevant because you can only select one of the choices. There is no need for them to be unique, so there are no additional validations. So for that one field I have just the in_place_edit for in the controller. I issue that after I loop through the content fields to issue the in_place_edit_with_validation_for.

    I have enough hooks in this now that I may just post my own blog entry on how I have mine all tied together.

    Thanks again for the extension to the in_place_edit.

  4. Hello! In spite of the including the full content of ‘in_place_macros_helper.rb’ I still get the error about invalid authenticity token. Does somebody know what may be the reason?

  5. gth: I have updated this page. Please get the new code for the plugin from the pasties link at the top. I was having issue with wordpress garbling large blocks of code. But I know the code from the pastie works.

  6. Hi Do you have an example site showing this working for a select box?

  7. I’m using this for a bulk edit form, with associations, and i’ve had to make the following change at like 20.
    replace
    @item.update_attribute(attribute, params[:value])
    with
    begin
    @item.update_attribute(attribute, params[:value])
    rescue
    @item.update_attribute(”#{attribute}_id”, params[:value])
    end

    then call the in place edit with the association type (’type’ rather than ‘type_id’)

    Also, from your assoc model, you can add the following

    def self.model_for_inplace_select
    “[” + Model.find(:all).collect{ |t| “[#{t.id},’#{t.name}’]” }.join(’,') + “]”
    end

    which you can then call to generate the select options.

    Thanks for the extension!

  8. Sorry, to clarify my last comment, you need to make the change on line 20 of the in_place_editing.rb file (found in the lib directory of the plugin)

    If anyone has a faster/more efficient way to find out if the attribute is a relationship, please do tell.

  9. Is it possible to use this on a select where you can add a new item

  10. Martin: It’s just a wrapper for the scriptaculous in place collection editor, which I don’t believe will let you add options. If you are looking for an alternative with more features, I suggest trying the dojo inline editor. See my dojo_helper plugin for more information.

  11. I had some trouble using this with a collection local in a partial. There’s no way to get an instance to the internal InstanceTag, and InstanceTag.object tries to find an object by using instance_variable_get(). What I wanted was to use the selector for each item in a collection, rendered in a partial. But the partial loop variable is a local, not an instance variable, so the InstanceTag ends up with a nil object.

    The only workaround I found was to tweak the code to let me pass an instance to in_place_editor_select_field explicitly. Is there a better way?

    (Also, it’s useful to have in_place_collection_editor forward the :value option, which it doesn’t currently.)

    Overall though, this saved me some time. Thanks for putting it together.

Leave a Reply