Today's the day

向软件大牛炫耀我会焊单片机,向硬件大牛炫耀我会写 Rails,向软硬件大牛炫耀我生物,向软硬件生物大牛炫耀我会折腾期货 -_-bbb

respond_to 新构想
is-Programmer 升级到 Chito 1.0.7

修改下 In Place Editing 插件

galeki posted @ 2008年8月21日 15:45 in RoR with tags rails in place editing , 3838 阅读

In Place Editing 这个插件代码非常简洁,却方便的实现了即时编辑的效果,不过自从从 Rails 的核心移出去之后,貌似就没有再升级过。

首先它不支持 Validate,In Place Editing 的保存不经过检查,即使你 validates_presence_of :name,也可以在 In Place Editing 中把 name 清空并保存,而且这样做了之后,由于 name 没有了文字,也没法点击编辑了。

另外它也不支持 protect_from_forgery。

虽说有个 REST In Place 插件,似乎是 In Place Editing 的替代品,其实稍微修改一下,In Place Editing 就能重放光芒了~

没法 validate 的缘故是因为它用了 update_attribute 的方法更新属性,只要改成 update_attributes 就可以了,然后检查保存是否成功,如果失败,将错误信息显示出来:

lib/in_place_editing.rb:

  1.     def in_place_edit_for(object, attribute, options = {})
  2.       define_method("set_#{object}_#{attribute}") do
  3.         @item = object.to_s.camelize.constantize.find(params[:id])
  4.         if @item.update_attributes({attribute.to_sym => params[:value]})
  5.             render :text => @item.send(attribute).to_s
  6.         else
  7.             render :text => @item.errors.on(attribute.to_sym)
  8.         end
  9.       end
  10.     end

也可以加个样式着重下错误信息:

  1. render :text => "<span style='color:red;'>#{@item.errors.on(attribute.to_sym)}</span>"

protect_from_forgery 的问题只要加上 authenticity_token 参数就可以了:

lib/in_place_macros_helper.rb:

  1.   def in_place_editor_field(...)
  2.     ...
  3.     in_place_editor_options[:url] = in_place_editor_options[:url] || url_for({ :action => "set_#{object}_#{method}", :authenticity_token => form_authenticity_token, :id => tag.object.id })
  4.     ...
  5.   end

 这样就 OK 了,效果还不错:

 

UMAIR 说:
2021年1月20日 18:18

JKBOSE Model Paper 2021: Sample papers not only gives a starting point to the students for where to begin but also helps in revising the vast syllabus within a time frame. Moreover, it serves equally well as a checklist which in turn helps to check whether all the topicsJKBOSE 12th Model Paper 2021 have been covered. And working smarty while revising the syllabus makes the difference between the toppers and among average students. So this time to help you to prepare for the exams from all aspects, here we are sharing all the possible sets of JK Board Previous Papers.

UMAIR 说:
2021年1月30日 14:13

You have done a great job on this article. It’s very readable and highly intelligent. You have even managed to make it understandable and easy to read. You have some real writing talent. Thank you sa gaming

UMAIR 说:
2021年2月02日 19:59

You have done a great job on this article. It’s very readable and highly intelligent. You have even managed to make it understandable and easy to read. You have some real writing talent. Thank you slilpp xyz shops search

UMAIR 说:
2021年3月10日 14:41

You have done a great job on this article. It’s very readable and highly intelligent. You have even managed to make it understandable and easy to read. You have some real writing talent. Thank you pakseo.net

Elastic Dori Manufac 说:
2021年9月01日 17:06

I love the humor your post has offered. I enjoyed this site a lot. Keep posting article like this. It is fun. Nice photography too!

slotxo ทดลองเล่น 说:
2021年9月13日 18:35

You have done a great job. I will definitely dig it and personally recommend to my friends. I am confident they will be benefited from this site

Disposable Scrub Sui 说:
2021年10月02日 18:26

This is exactly what I was looking for. Thanks for sharing this great article! That is very interesting Smile I love reading and I am always searching for informative information like this!

Dalvin Smith 说:
2021年10月22日 13:23

That gives off an impression of being brilliant anyway i am still not very beyond any doubt that I like it. At any rate will look significantly more into it and choose by and by! type iir surgical disposable face mask surgical disposable mask Disposable Scrub Suits elastic earloop earloop elastic

Dalvin Smith 说:
2021年11月24日 17:49

Your blogs further more each else volume is so entertaining further serviceable It appoints me befall retreat encore. I will instantly grab your rss feed to stay informed of any updates.
<a href="https://sinparty.yolasite.com/">free porn party</a>

Soghat e Sheerin 说:
2022年3月29日 18:47

This is some thing very informative and point to point. There is no round and round in this article. Like this smplicity . This clears my old point of view. THANKS Soghat-e-Sheerin Soghat Sweet Soghat Bakers Saloni Soghat Soghat Pizza

Dalvin Smith 说:
2022年4月24日 17:33

That gives off an impression of being brilliant anyway i am still not very beyond any doubt that I like it. At any rate will look significantly more into it and choose by and by! The Hiltonian Business News Entertainment News World News

Dalvin Smith 说:
2022年4月24日 22:55

I haven't any word to value this post.....Really i am awed from this post....the individual who make this post it was an extraordinary human..thanks for imparted this to us. The Hiltonian The Hiltonian World News Entertainment News World News

M Anus Raza 说:
2022年5月23日 07:38

That gives off an impression of being brilliant anyway i am still not very beyond any doubt that I like it. At any rate will look significantly more into it and choose by and by! Breaking News Breaking News Breaking News Breaking News Breaking News

Softech Source 说:
2022年9月05日 18:41

Nicely well-written article. It was an awesome article to read. Complete rich content and fully informative Softech Source App Development Company Web Development Company Ecommerce Website Development Company Best Digital Marketing Company SEO

Dalvin Brown 说:
2022年9月05日 19:37

Nicely well-written article. It was an awesome article to read. Complete rich content and fully informative Softech Source App Development Company Web Development Company Ecommerce Website Development Company Best Digital Marketing Company SEO

The Hiltonian 说:
2022年9月19日 14:27

Nicely well-written article. It was an awesome article to read. Complete rich content and fully informative Business News World News Entertainment News Tech News Health News

The Hiltonian 说:
2022年9月20日 00:23

Nicely well-written article. It was an awesome article to read. Complete rich content and fully informative tranny tube shemale tubes skyward fbisd fbisd skyward cam newton net worth 2021

News Record 说:
2022年9月26日 10:18

This is some thing very informative and point to point. There is no round and round in this article. Like this smplicity . This clears my old point of view. THANKS
<a href="https://newsrecord.co.uk/shooting-games-unblocked/">unblocked shooting games</a>
<a href="https://newsrecord.co.uk/bird-box-2-what-to-expect-from-the-upcoming-sequel/">bird box 2 release date uk</a>
<a href="https://newsrecord.co.uk/bird-box-2-what-to-expect-from-the-upcoming-sequel/">bird box 2 release date</a>
<a href="https://newsrecord.co.uk/behind-the-ear-tattoos/">behind the ear tattoos men</a>
<a href="https://newsrecord.co.uk/behind-the-ear-tattoos/">ear tattoos men</a>

News Record 说:
2022年9月26日 10:18

This is some thing very informative and point to point. There is no round and round in this article. Like this smplicity . This clears my old point of view. THANKS unblocked shooting games bird box 2 release date uk bird box 2 release date behind the ear tattoos men ear tattoos men

Getmuzz 说:
2022年10月24日 17:56

Breaking News & Top Stories. Latest breaking news and information on the top stories, weather, business, entertainment, politics and many more. Getmuzz

Resume Builder 说:
2022年11月23日 18:37

There are numerous dissertation websites on-line because you additionally obtain obviously stated inside your web site.
[url=https://resumebuilderr.com/]Resume Builder[/url]

Hira Mart 说:
2022年11月30日 18:50

We Provide Halal Products Only! Hira Mart All products in our shop are Muslim-friendly, so you can buy with confidence

Hira Mart 说:
2022年12月04日 14:57

We Provide Halal Products Only! Hira Mart All products in our shop are Muslim-friendly, so you can buy with confidence


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter