【发布时间】:2010-02-13 20:26:00
【问题描述】:
(Rails 新手)
你好!
我觉得我正在重用我的很多代码,我觉得必须有更好的方法来做到这一点......(我确信有......)
我有一个设置页面,您可以在其中创建类别和程序(属于一个类别)。
索引设置操作:
def categories_and_procedures
@prefs = @current_practice.preferences
@category = @current_practice.categories.build
@categories = @current_practice.categories.all
@procedure = @current_practice.procedures.build
end
视图中有一个包含所有当前类别的列表和一个用于创建新类别的表单。在 Category 模型中是一个验证(validates_uniqueness_of :name)。
创建动作:
def create_category
@category = @current_practice.categories.build(params[:category])
if @category.save
flash[:notice] = "New category created: <i>#{@category.name}</i>"
redirect_to :action => "categories_and_procedures"
else
##Duplicate code!!!!!!
@prefs = @current_practice.preferences
@category = @current_practice.categories.build
@categories = @current_practice.categories.all
@procedure = @current_practice.procedures.build
##Duplicate code!!!!!!
render :action => "categories_and_procedures"
end
end
有没有办法可以将它移动到我可以调用的函数中?帮手?过滤器? 我不知道。
谢谢!
【问题讨论】:
标签: ruby-on-rails forms validation