【发布时间】:2011-07-17 19:50:35
【问题描述】:
鉴于这些模型:
class Person
end
class Man < Person
def edit
@person = Man.find(params[:id])
respond_to do |format|
format.js { render :template => "man_form" } }
end
end
end
class Woman < Person
def edit
@person = Woman.find(params[:id])
respond_to do |format|
format.js { render :template => "woman_form" } }
end
end
end
然后在每个表单中,我使用这个由所有人共享的辅助方法创建一个“特征”的“选择”:
def person_select(person, options)
select :person, "characteristics", options
end
我将如何做到这一点,以便当它被一个子类(男人或女人)调用时,它会创建具有该模型名称的选择名称,而不是父“人”?
所以,我希望能够打电话:
person_select(@man_object, {...})
然后得到:
<select name="man[age]" id="man_age">
...
</select>
我需要这样做的原因是,当提交表单时,我可以从 man_controller 获取 params[:man] 或从 woman_controller 获取 params[:woman] 并能够使用这些特定的对象类型,并且不是“人”。
我可以获得类名并执行以下操作: 选择 person.class.to_s.underscore 但它不再使用该对象,因此不会在列表中选择传递的对象。
谢谢!
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 inheritance