使用一种形式的 Rails STI
我有一个表单,允许我将不同格式的文件添加到流中。因此,流由许多文件组成,这些文件是 XML 文件,但基本上具有不同的架构。我有一种表单允许用户添加他们想要的任何文件,我正在使用 STI(当数据已在表中时效果很好),我的麻烦是将数据添加到表中。
该表单有 1 个输入字段,只有一个 file_field 允许用户选择他们想要上传的文件。由于我只有一种形式,因此无法实例化正确的对象,因此我必须以编程方式执行此操作......而且我不知道如何执行。
我是否只是(或者可以)添加一个包含可能类型的下拉列表,并将该字段称为“类型”,以便在提交表单时,rails 将实例化对象的写入类型,因为提供了类型属性?
对此的最佳实践是什么..我正在运行 Rails 2.3.4。
I have a form that allows me to add files of different formats to a stream. So, a stream is made up of many files, these files are XML files but basically have different schemas. I have one form that allows the user to add whatever file they want, I am using STI (which works great when data is already in the table), my trouble is adding the data to the table.
The form has 1 input field, just a file_field that allows the user to select the file they want to upload. Since I only have one form I'm not able to instantiate the correct object, I have to do it programatically.. and I'm not sure how to do it.
Do I just (or can I) add a drop down with the possible types, and call that field 'type' so that when the form is submitted rails will instantiate the write type of object because the type attribute is provided?
What is the best practice for this.. I'm running rails 2.3.4.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在 http://coderrr.wordpress.com/2008/04/22/building-the-right-class-with-sti-in-rails/#comment-1826
这对我来说非常有用最少的代码 - 我不知道它是否很黑客,但它可以工作,而且相当干净。我喜欢它只有 10 行左右的代码。
I found a solution at http://coderrr.wordpress.com/2008/04/22/building-the-right-class-with-sti-in-rails/#comment-1826
Which worked great for me with minimal code - I don't know if its hackish or not but it works, and is rather clean. I liked the fact that its only 10ish lines of code.
我不知道你有多少类型,但我过去只是对不同类型使用单独的控制器和视图。这样,您就不会创建基类的新对象并尝试设置类型,您只需使用从基类继承的模型。资源的每个新/编辑页面都可以在 form_for 块中呈现共享部分。该部分将包含您的 file_field。
这样,当提交表单时,它将转到正确的控制器,调用正确的resource.new,一切正常。
当然,缺点是文件较多,无论您链接到“添加新文件”的任何页面,都需要添加多个链接,例如“添加新的这种类型的文件”、“添加新的这种类型的文件”等。
至于设置输入表格我不确定这是否有效,我对此表示怀疑,但请尝试一下(让我们知道)。您可以将该类型下拉为 select_tag,并在更改时使用 Javascript 更改表单上的操作位置。
编辑并添加了基本工作
并不是说我喜欢这个解决方案&我怀疑它绝不是最好的,但如果你真的不想要单独的控制器并且需要让它工作,你可以这样做:
I don't know how many types you have but I have simply used separate controllers and views for the different types in the past. This way you don't create new object of the base class and try to set the type, you just use the model that inherits from the base class. Each new/edit page for your resources can render a shared partial in the form_for block. The partial would contain your file_field.
This way when the form is submitted it will be going to the correct controller, calling the correct resource.new and everything is ok.
The drawback of course is more files and whatever page you are linking to "add new file" on you need to add multiple links like "add new this type of file", "add new that type of file" etc.
As for setting the type in the form I'm not sure whether that works, I doubt it but just give it a try (Let us know). You could make that type drop down a select_tag and when changed use Javascript to change the action location on the form.
Edited and added basic work around
Not that I like this solution & I doubt its by no means the best, but if you really don't want separate controllers and you need to get it working you could do something like this: