原创作者: hideto   阅读:1414次   评论:0条   更新时间:2011-05-26    
我们习惯与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不好合并,以为后者是对应到上述不同的方法的
评论 共 0 条 请登录后发表评论

发表评论

您还没有登录,请您登录后再发表评论

文章信息

Global site tag (gtag.js) - Google Analytics