`
hideto
  • 浏览: 2650802 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

每天一剂Rails良药之Cleaning Up Controllers with Post-back Actions

    博客分类:
  • Ruby
阅读更多
我们习惯与one-action-per-request,但是我们(特别是初学者)很容易被controller里的new(),create(),edit()和update()这些方法弄晕。
其实我们可以用一个方法来代替them all:
def edit
  @recipe = Recipe.find_by_id(params[:id]) || Recipe.new
  if request.post?
    @recipe.attributes = params[:recipe]
    redirect_to :main_url and return if @recipe.save
  end
end

这证明了Ruby语言的强大和灵活
但和RESTful Rails Development不好合并,以为后者是对应到上述不同的方法的
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics