Tag Archives: jrails

RESTful in-place edit in Rails and jRails/jQuery

I’ve been using jRails in my recent Rails projects, the original Rails in-place editing plugin uses script.aculo.us, there is a jRails version of it, but neither of them is RESTful – they both create extra actions to update the in-place edit fields.

I found janv’s rest_in_place plugin and it uses the default update action to update the field, so no routes modifications are necessary. I had some problems with the plugin at first but after a pull-request correspondence the plugin now works well. Here are the highlights on how to use it, keep in mind that I use HAML.

The plugin’s init.rb doesn’t load anything for you, so you have to go into your application layout and include the js file:

    = javascript_include_tag 'jquery.rest_in_place.js'

If you have CSRF protection on, this plugin also requires you to set a javascript var. If you have jRails it automatically append the token in ajax requests, but then you would have to modify the plugin a bit to get it to work.

:javascript
  rails_authenticity_token = '#{form_authenticity_token}'

In your controller’s show action, handle the javascript response:

  def show
    respond_to do |format|
      format.html # show.html.erb
      format.js   { render :json => @model }
    end
  end

then you can render the helper in the views:

- div_for @model do
  %p
    = label_tag "Name"
    %br
    %span.rest_in_place{ :attribute =>'name' }
      =h @model.name
  %p
    = label_tag "Location"
    %br
    %span.rest_in_place{ :attribute => 'location' }
      =h @model.location

Rails plugin/helper to provide “click to copy text field” (to clipboard)

You often see sites with text fields of share links in various different formats, like Skitch provides links to share the images you skitch, they look like this:

Skitch.com > andrewng > Energy Saver
Uploaded with plasq‘s Skitch!

You often click the text box or a copy button next to it and the content is copied to your clipboard so you can paste it into a tweet, a blog or forum post and what not.

I needed something like this for one of my projects, it is pretty easy to do, and there are things you can use to do this. However there isn’t a super simple way to do this in Rails, maybe it’s so simple that nobody bothered to publish a plugin. Well, just for kicks I’ve created a plugin that lets you do exactly this by using a cilick_to_copy_text_field_tag helper in your views. This plugin requires jQuery or jRails, neither of which is part of standard Rails, but they are pretty easy to add to any Rails project.

The plugin is hosted here: http://github.com/ayn/click_to_copy_text_field/tree/master, the README pretty much tells all you need to know to use it.