Today's the day

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

用 Texvc 生成数学公式
修改下 In Place Editing 插件

respond_to 新构想

galeki posted @ 2008年8月01日 10:03 in RoR with tags rails respond_to , 3911 阅读

respond_to 非常好用,可以根据不同的 format 来选择不同的应答策略,并且自动设置相应的 MIME 值,看上去也非常爽目。

在 Blog 系统中,比如文章的 RSS,就可以:

  1. def index
  2.    @posts = Post.find(:all)
  3.    respond_to do |format|
  4.        format.html { ...}
  5.        format.rss {...}
  6.    end
  7. end

这样比在单独构建一个 RSS action 要好得多~

不过,如果 Blog 通过 html 显示出来的话,还需要做其他一些事情,比如构建 Blog 的各个边栏,这些事情一般都放在 before_filter 中去做:

  1. class PostsController < ApplicationController
  2.     before_filter :get_sidebars
  3.     ...

但是这些工作在 RSS 中完全不需要,那么怎么在 RSS 忽略掉这些工作呢?

一个选择就是去掉 before_filter,把 get_sidebars 写入到 format.html { } 中去,这样的话,PostsController 里面每个 action 的 format.html 都要这样做,不是啥好办法。

第二个选择就是在 get_sidebars 里面检测 format:

  1. def get_sidebars
  2.     if params[:format] == "html"
  3.        ...create sidebars

这样似乎还算 ok,其他的方法还没有想出来……

前两天,正好看到 maiha 写的新 respond_to 表示方法的构想,去掉 respond_to 那个 block,而按照 aciton.format 的格式,把不同 format 的处理,放到单独的函数中去:

  1. def index
  2.     @posts = Post.find(:all)
  3. end
  4.  
  5. def index.html
  6.     ...
  7. end
  8.  
  9. def index.rss
  10.    ...
  11. end

这样看上去比 respond_to 清晰了很多,如果可以实现,那么如果在 before_filter 里面也可以:

  1. before_filter :get_sidebars, :only => "*.html"

就能完美的解决上面的问题了~

meidir 说:
2022年8月20日 16:40

I just wanted to tell you how much my partner and i appreciate anything you’ve discussed to help improve the lives of men and women in this subject matter. Through your current articles, I have gone through just a newcomer to a professional in the area. It’s truly a gratitude to your good work. 소울카지노주소

meidir 说:
2022年8月23日 17:58

Most what i read online is trash and copy paste but i think you offer something different. Keep it like this. 바카라사이트

meidir 说:
2022年8月26日 19:26

After examine a couple of of the weblog posts in your website now, and I truly like your manner of blogging. I bookmarked it to my bookmark website list and might be checking back soon. Pls take a look at my website as effectively and let me know what you think. [url=https://www.pigcasino.net/]온라인바카라[/url]

meidir 说:
2022年8月26日 19:27

After examine a couple of of the weblog posts in your website now, and I truly like your manner of blogging. I bookmarked it to my bookmark website list and might be checking back soon. Pls take a look at my website as effectively and let me know what you think. 온라인바카라

meidir 说:
2022年12月05日 00:16

I would like to thank you for the efforts you’ve put in writing this web site. I am hoping the same high-grade blog post from you in the upcoming also. In fact your creative writing skills has encouraged me to get my own blog now. Actually the blogging is spreading its wings rapidly. Your write up is a good example of it. 二手Macbook

 

============

 

Rattling clear site, regards for this post. 메이저사이트

meidir 说:
2022年12月23日 02:18

I had a great stretch reading about your send as I read it extensively. Superlative calligraphy! I am looking disrespectful to hearing more from you محامي عمالي

 

=============

 

Something more important is that when searching for a good on-line electronics store, look for online stores that are constantly updated, maintaining up-to-date with the most up-to-date products, the perfect deals, in addition to helpful information on products. This will ensure that you are doing business with a shop that really stays ahead of the competition and provide you what you should need to make intelligent, well-informed electronics purchases. Thanks for the important tips I’ve learned through the blog. افضل محامي في الرياض

 

==============

 

“I wish to show thanks to you for rescuing me from this particular predicament. Just after researching through the search engines and coming across techniques which were not pleasant, I believed my entire life was gone. Being alive without the solutions to the problems you’ve solved through the review is a critical case, and those that could have adversely affected my career if I had not discovered your web page. Your personal ability and kindness in playing with almost everything was invaluable. I’m not sure what I would’ve done if I hadn’t discovered such a stuff like this. I’m able to now relish my future. Thank you very much for your reliable and results-oriented guide. I won’t think twice to propose the blog to any person who needs and wants guide about this subject.” محامي تجاري في الرياض

 

==============

 

You actually make it appear really easy with your presentation however I in finding this topic to be really one thing which I think I would by no means understand. It sort of feels too complicated and very vast for me. I am looking forward on your next post, I’ll attempt to get the hang of it! محامي متخصص في القضايا الجنائية

 

==============

 

By marketing in the comment box, additionally you generate back again inbound links to your webpage. These back again inbound links will very last indefinitely if they can endure webmaster moderation. This opens an avenue for potential customers generation which will final indefinitely. استشارات قانونية مجانية جدة

meidir 说:
2023年1月11日 16:56

Very nice post. I just stumbled upon your weblog and wanted to say that I have truly enjoyed surfing around your blog posts. After all I will be subscribing to your rss feed and I hope you write again soon! 온라인바카라

meidir 说:
2023年2月18日 22:30

Hi, i read your blog from time to time and i own a similar one and i was just wondering if you get a lot of spam comments? If so how do you protect against it, any plugin or anything you can advise? I get so much lately it’s driving me crazy so any assistance is very much appreciated. 슈어맨

meidir 说:
2023年3月19日 18:02

“There are certainly many more details to take into consideration” 온라인카지노

meidir 说:
2023年4月03日 23:44

Just a smiling visitant here to share the love (:, btw outstanding pattern . 手機回收

 

==================

 

That is some inspirational stuff. Couldn’t know that opinions can be this varied. Thank you for most of the enthusiasm to consider such tips here. MacBook回收

 

==================

 

Hello, you used to write great, but the last several posts have been kinda boring… I miss your great writings. Past several posts are just a little bit out of track! come on! 三星回收價目

 

==================

 

I was on Twitter looking for Neiko Tools Cordless Tools when I found a link to this blog, happy I stopped by – Cheers iphone回收價

meidir 说:
2023年4月21日 22:44

This is actually the kind of information I have been trying to find. Thank you for writing this information. reputation defenders

meidir 说:
2023年4月22日 03:18

Remarkable post and will look forward to your future update. Do you accept advertisers? it support Singapore

meidir 说:
2023年5月03日 00:06

Thanks for taking the time to discuss that, I feel strongly about this and so really like getting to know more on this kind of field. Do you mind updating your blog post with additional insight? It should be really useful for all of us. free chatgpt prompts

meidir 说:
2023年6月07日 01:30

I consider something truly interesting about your weblog so I saved to fav. disfuncion temporomandibular

 

 

================

 

 

I just want to complete a quick comment as a way to express gratitude to you personally for all those wonderful pointers you’re posting at this site. Time consuming internet investigation has by the end for the day been rewarded with top quality strategies to tell my guests. I might say that many people guests can be extremely endowed to happen in an amazing network with methods . marvellous individuals with useful hints. I feel quite privileged to obtain used your webpages and check toward really more fabulous minutes reading here. Thank you for most things. Kaban

 

 

==================

 

 

My friend first found your blog on Google and she referred your blog to me . https://rossellcarol.com/

meidir 说:
2023年7月10日 22:21

Perhaps you should update the php server on your webhost, WordPress is kinda slow. invest online

meidir 说:
2023年7月28日 00:08

very good put up, i certainly love this website, carry on it macbook air二手回收價

 

 

=======================

 

 

I think you have a nice site here… i just happened to find it doing a yahoo search. anyway, excellent post.. i’ll be bookmarking this page for certain. 手機回收

 

 

=======================

 

 

Very informative blog. I especially enjoy content that has to do with beauty and fitness, so it’s refreshing to me to see what you have here. Keep it up! facial exercises 魔術表演

 

 

=======================

 

 

Hai bisogno di scaricare musica no cost?! Visita questo sito world wide web. Cerchi musica gratis su web? 花藝師

 

 

=======================

 

 

Hmm it appears like your site ate my first comment (it was super long) so I guess I’ll just sum it up what I submitted and say, I’m thoroughly enjoying your blog. I as well am an aspiring blog blogger but I’m still new to the whole thing. Do you have any recommendations for novice blog writers? I’d certainly appreciate it. 花藝設計

 

 

==========================

 

 

Make sure you excuse my my English speak, i’m simply schooling. I very much like your web site greatly, I realize it quite fascinating and that i saved a bookmark in my personal web. 插花課程

 

 

===========================

 

 

There is noticeably big money comprehend this. I suppose you’ve made particular nice points in functions also. 花藝學校

meidir 说:
2023年8月04日 00:02

Definitely believe that which you said. Your favorite justification appeared to be on the net the simplest thing to be aware of. I say to you, I certainly get annoyed while people consider worries that they plainly don’t know about. You managed to hit the nail upon the top and defined out the whole thing without having side-effects , people can take a signal. Will probably be back to get more. Thanks https://lightinfitness.com

meidir 说:
2023年8月26日 23:58

You for sure understand what you’re referring to,this blog is simply excellent . digital alliance

meidir 说:
2023年8月31日 00:09

Eventuellement je peux vous transférer les adresses pour plus de picto à propos de cet idée. Joignez moi directement… 강남안마

meidir 说:
2023年9月03日 22:10

Hello! I simply want to give you a huge thumbs up to the wonderful information you have here on this post. We are coming back to your blog site to get more soon. Australian Association of Franchisees

meidir 说:
2023年10月13日 11:12

Oh my goodness! a wonderful article dude. Many thanks However I am experiencing trouble with ur rss . Don’t know why Unable to sign up to it. Perhaps there is everyone getting identical rss issue? Anyone who knows kindly respond. Thnkx 울산룸싸롱

meidir 说:
2023年10月13日 13:44

Whats up. Very nice website!! Man .. Excellent .. Superb .. I’ll bookmark your website and take the feeds additionally…I am satisfied to find numerous useful info right here within the post. Thanks for sharing. medical issues

meidir 说:
2024年1月21日 05:07

Hey, I simply hopped over in your web page by means of StumbleUpon. Not one thing I might in most cases learn, however I favored your feelings none the less. Thank you for making something price reading. AI Agents in Project Management

Meidir 说:
2024年7月18日 04:27

Kudos for posting such a useful blog. Your blog is not only informative but also very artistic too. There usually are extremely couple of individuals who can write not so easy articles that creatively. Keep up the good writing !! 2024 pictures of john spencer ellis

 

 

===============

 

 

Thanks a bunch for sharing this with all of us you actually know what you’re talking about! Bookmarked. Kindly also visit my web site =). We could have a link exchange agreement between us! john spencer ellis las vegas


登录 *


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