It’s just data

Rest In Place

Edit in place is a handy feature where clicking on an area of a web page replaces that portion of the page with a form which will enable the user to update that information.  At one time, this was a part of Rails, but in 2007 it moved out to a plugin, and improved upon.  The one that looked like it most closely matched my needs was REST in Place.  It comes in three flavors, I picked JQuery.

URLencoding

The first issue I had was that I would not be able to authenticate, despite carefully following the instructions for setting a rails_authenticity_token.  Looking at it from the server logs, it appeared that the token received had a space in it.  Looking at it from the client, I found a plus in the same place.  Looks like request wasn’t properly x-www-form-urlencoded. Looking at the source (line 18) verifies this to be the case.  A call to encodeURIComponent would solve this.  So would a call to CGI.escape in the assignment to rails_authenticity_token.  The former seems like the “right” way, but instead of modifying the gem, I decided to take the latter approach for now.

It is not clear to me why this problem wouldn’t affect others enough to have been noticed and fixed.

JSON

Once this was fixed, I could edit information in place.  The trouble is that the formatting was lost after the edit.  The information I was editing at the time was a dollar amount, something that I format with number_to_currency.  It seems that under the covers, after an update is made, the information is re-fetched using JSON and that’s the information that is displayed.  That information is “raw” and essentially straight from the database.  My first attempt to address this was to create new attributes which were not backed by the database; but by default such attributes are not included in the JSON so instead of simply losing the formatting, now I lost the result entirely.  This could be addressed, but I decided to go back to the original attributes, and centralize the formatting.

class ActiveRecord::Base
  def self.dollarize *attrs
    attrs.each do |name|
      define_method name do
        clone = SimpleDelegator.new(self[name])
        clone.extend(ApplicationHelper)
        def clone.to_s
          number_to_currency self, :unit=>''
        end
        def clone.to_json(opts)
          to_s.inspect
        end
        clone
      end

      define_method "#{name}=" do |value|
        self[name.to_sym]=value.gsub(/[^0-9.]/, '').to_f
      end
    end
  end
end

This allows fields to be declared as dollarized, after which point all formatting is taken care of automatically, and all other basic numeric functionality is delegated back to the original Float.

Kinda odd to have ApplicationHelper included within a model, but the amount of lines of code went down, processing became more consistent, so this was totally a net plus.

Caching

Now that the basic functionality worked, I noticed something odd.  If after doing an in-page-edit I clicked on a link and subsequently clicked on the back button, I was presented with JSON instead of the HTML page I was expecting.

I’m not totally sure what is going on here, as the server was properly sending private, max-age=0, must-revalidate as the cache control header.  The only thing I can figure is some oddity with Firefox 3.0.11 with XMLHttpRequeset and https sessions.  I’m not a big fan of conneg for this reason.  When it works, it is very useful.  When it doesn’t, it is often difficult to diagnose and debug.

But I have a known workaround.  By appending a question mark to the URI used for XHR, the client sees it as a different resource and the server processes it the same.

Helper

Wanting to apply the same workaround consistently across my application was the tipping point.  The markup rest_in_place wasn’t all that complicated, but was repetitive.  Time for a helper function:

# Usage:
#   <% rest_in_place(obj) do |rip| -%>
#     ... markup ...
#     <%= rip.editable :field %>
#     ... markup ...
#   <% end -%>

def rest_in_place(obj)
  yield RIP_Builder.new(url_for(obj), obj)
end

class RIP_Builder
  def initialize(url, obj)
    @url = url + "?"
    @obj = obj
    @name = ActionController::RecordIdentifier.singular_class_name(obj)
  end

  def editable(field)
    "<span class='rest_in_place' object='#{@name}' attribute='#{field}' url='#{@url}'>#{@obj.send field}</span>" 
  end
end

Attributes

One final note, only of interest to pedants.  The markup above is not conformant in that it invents new attributes.  In order to satisfy the markup orthodoxy, I’d suggest modifying RIP to accept synonyms for each attribute that happen to start with data-.  I would certainly use it on all public facing websites that made use of this feature.  As to what I use on my own private network, that’s between me and my server.


Hey Sam,

thanks for your suggestions. I’ll try to implement them as soon as I find the time
(finishing my diploma thesis currently). If you already made the changes, I’d be very
glad for patches (or fork and send a pull request).

kind regards

Jan

Posted by Jan Varwig at

Wow: that was quick!

Not all of these were suggestions, and the topic of formatting requires more thought; but I will fork, make changes, and send a pull request when complete.  At the very least, some of these items can be added to the documentation.

Posted by Sam Ruby at

submitted by ossreleasefeed [link] [0 comments]...

Excerpt from programming at

[link]...

Excerpt from Delicious/Vathek at

WRT caching - is Vary being sent?

Posted by Mark Nottingham at

Doesn’t look like it.  This is via FireBug:

Date: Sun, 14 Jun 2009 22:13:04 GMT
Server: Apache/2.2.8 (Ubuntu) PHP/5.2.4-2ubuntu5.6 with Suhosin-Patch mod_ssl/2.2.8 OpenSSL/0.9.8g Phusion_Passenger/2.2.2
X-Powered-By: Phusion Passenger (mod_rails/mod_rack) 2.2.2
X-Runtime: 119
Etag: "4b048a6b713b83272cd80b9e1a266c95"
Cache-Control: private, max-age=0, must-revalidate
Content-Length: 153
Status: 200
Keep-Alive: timeout=15, max=98
Connection: Keep-Alive
Content-Type: text/javascript; charset=utf-8
Posted by Sam Ruby at

Sam, re. caching issue, see bug 388141. No attention in the 2 years since I filed it. Not even confirmed.

Mark: yes. Firefox ignores it.

If I look at that list of issues, I’d suggest you try entering some non-US-ASCII characters next (e.g. a € euro sign), it seems to be the next thing to expect not to work. Too often people pay too little attention to escaping and encoding :(.

Posted by Laurens Holst at

So to make things clear, as I see in this particular case a Vary header is not sent: adding a Vary header, although it of course should be there, will make no difference to Firefox’s cache and the issue will still be there.

Posted by Laurens Holst at

It is my belief that if one says “DO NOT CACHE!!!”, there is no requirement to additionally say “oh by the way, the content varies by value the accept header”.

Posted by Sam Ruby at

Doesn’t have to be a belief: [link] (I.e. it’s a simple fact, though you can still do it to inform the user agent.)

Posted by Anne van Kesteren at

Sam Ruby: Rest In Place

Sam Ruby: Rest In Place...

Excerpt from Anarchogeek Tumblr at

Use custom data attributes as suggested by Sam Ruby

m README.markdown m javascripts/jquery.rest_in_place.js m javascripts/mootools.rest_in_place.js m javascripts/rest_in_place.js m testapp/app/views/users/show.html.erb Use custom data attributes as suggested by Sam Ruby See...

Excerpt from Recent Commits to rest_in_place:33615b0f96fd3f9c61db67c9023d59ed3f357ee4 at

Use custom data attributes as suggested by Sam Ruby

m README.markdown m javascripts/jquery.rest_in_place.js m javascripts/mootools.rest_in_place.js m javascripts/rest_in_place.js m testapp/app/views/users/show.html.erb Use custom data attributes as suggested by Sam Ruby See...

Excerpt from Recent Commits to rest_in_place:master at

Sam Ruby: Rest In Place

[link]...

Excerpt from Delicious/tag/rails at

I like your all post. You have done really good work. Thank you for the information you provide, it helped me a lot. I hope to have many more entries or so from you.

Posted by fnaf sister locatio at

I found lots of interesting information here.Great work
Thanks for the share loved reading the article, please do share more like this wiht us .

Posted by Temple Run 2 at

Hi, Thanks you to create this useful information. I joined your blog to get help

Posted by Govt Jobs at

gvcvbc

Posted by bvczvxc at

I really enjoyed reading your article. I found this as an informative and interesting post, so i think it is very useful and knowledgeable. you should have create more website through Godaddy Coupons. it will be more efficient and helpful for all.

Posted by Godaddy promo code at

Thank your site! information your share is useful to me!
games for kids | happy wheels

Posted by abcya at

Thank your site! information your share is useful to me!
games for kids | wings.io

Posted by happy wheels the game free at

thanku for sharing this post..
<a href=http://www.10thdatesheet.in/up/10th-date-sheet-high-school-time-table> UP board 10th date sheet 2017 </a>
<a href=http://www.12thdatesheet.in/mp-board/12th-date-sheet-xii-time-table-2/> MP board 12th date sheet 2017 </a>
<a href=http://madhya-pradesh.board-results.in/mp-board-10th-date-sheet/>  MP 10th class time table 2017 </a>
<a href=http://schools.chekrs.com/up-board-12th-date-sheet.html> UP Board 12th Exam Scheme 2017 </a>

Posted by aanchal at

Central Government Jobs: Central government jobs pertain to departments like central soil conservation department, income tax department, Central Railways, and other departments like judiciary, executive etc.

State Government Jobs: State Government Jobs in India include financial institution, educational institution, banking institution, judiciary, and other departments like Forest and Animal husbandry etc.

Government Jobs

10th pass govt jobs

12th pass govt jobs

Govt Jobs in Delhi

Government Jobs in Maharashtra

Government Jobs in UP

Latest Govt Jobs

Sarkari Naukri

Bank Jobs

Defence Jobs

Railway Jobs

Posted by Government Jobs at

Really good information on the topic

Thanks to All
SARKARI RESULT

Posted by Sarkari Result at

it’s great that I known this site! Can you sharing some updates on how you have made this powerful post!

Posted by fb login at

Thanks for your post! I hope you will continue to make similar posts like this! It is really necessary for those who want to learn about them!

Posted by may phat dien at

Thanks for sharing. I hope it will be helpful for too many people that are searching for this topic.

Posted by cool math games at

Thanks for this post

Windows Softwares

Mac Softwares

I hope it will be helpful for too many people that are searching for this topic.

Posted by 3uru at

Nice post!..good information,it is really helpful..it really impressed me alot and i just loved it.Thanks for posting such an informative content.
Russian Escorts in Goa

Posted by Goa Best Escorts at

Passion is similar to two aggregate the best safety blues band, sprained is invariably unwilling to forget about a single!

Posted by shoes in stock at

I glad to be here read this great stuff, its helping to my study and my work. Thanks for share with us.

Posted by Escorts in Bangalore at

Thank your site! Can you sharing some updates on how you have made this powerful post!
happy wheels | fb login

Posted by abc ya at

Passivity is milquetoast storage cache.

Posted by shoes in stock at

Thanks for sharing

Posted by Dani at

If you decide to extremely feel you’re a child, silent should waste time, you’ll implement actually nothing, current that grouse.

Posted by shoes in stock at

Thanks for share your knowledge with me. Usually I do not read everything on internet but you are an exceptional. I always read your blog. Thanks for your guidance.

Government Jobs | Bank Jobs | Railway Jobs | SSC Online | Employment News

Posted by Ravi Teja at

Exploring a better world for providing this marvelous information which i need to know more. Therefore I think there is no harm if we change the unhealthy lifestyle that we’ve run with a healthy lifestyle. Thanks obat asam lambung

Posted by Obat Asam Lambung at

A partnership offers a lot of anguish, received offer you so much delight.

Posted by shoes in stock at

This was such an amazing post keep sharing this thank you

Posted by cupshe fashionable products at

I enjoyed reading your article good information on the topic

Posted by Govt jobs 2017-18 at

watch the Oscars 2017 Online Free

Oscars 2017 live stream Free

Oscars Live Stream Free

Oscars 2017 Live Stream

Oscars 2017 Live Stream

Watch Academy Awards 2017 Live

http://www.watchoscarslivestreaming.com/2017/02/89th-academy-awards-oscar-nominees-nominations-2017-full-list-best-picture.html

Posted by ywaer at

watch the Oscars 2017 Online Free

Oscars 2017 live stream Free

Oscars Live Stream Free

Oscars 2017 Live Stream

Oscars 2017 Live Stream

Watch Academy Awards 2017 Live

http://www.watchoscarslivestreaming.com/2017/02/89th-academy-awards-oscar-nominees-nominations-2017-full-list-best-picture.html

Posted by ywaer at

Keep up the great work!

Posted by New Skate Shoes at

I think it be will helpful for too many people that is searching for this topic.

Posted by Ariana at

I love your ideas and your thoughts, it will be very helpful for me. Thanks for share that.
Model Escorts Service Jaipur

Posted by Suhana Khan at

I was amazed by you. the way you create a website very thorough and good. This is very very impressive.
Call Girls Service Dwarka

Posted by Lila at

I really enjoyed reading your article. I found this as an informative and interesting post, so i think it is very useful and knowledgeable.
Call Girls South Delhi

Posted by Salima at

nice article

Posted by jio 4g voice apk at

I like very much to read Your posts and this is for me absolutely number one blog in this topic thanks for sharing this post.

Posted by Escorts in 5 Star Hotel in Gurgaon at

Add your comment