Ruby on Rails:是否有一个标准帮助程序,用于简化实现创建和更新操作?

Ale*_*xey 3 action controller ruby-on-rails

在rails中有一些像这样的标准辅助方法:

def standard_save model
  model_sym = model.class.name.underscore.to_sym

  model.update_attributes params[model_sym]
  if model.save
    yield
    redirect_to model
  else
    render :new
  end
  model
end
Run Code Online (Sandbox Code Playgroud)

你会这样使用:

def create
  standard_save(@user = User.new) {
    flash[:success] = "You account was successfully created"
  }
end

def update
  standard_save @user = User.find(params[:id])
end
Run Code Online (Sandbox Code Playgroud)

Rya*_*igg 5

还有的inherited_resources你可以使用这个宝石.