Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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. </p> <p>This way when the form is submitted it will be going to the correct controller, calling the correct resource.new and everything is ok.</p> <p>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.</p> <p>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.</p> <p><strong>Edited and added basic work around</strong></p> <p>Not that I like this solution &amp; 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:</p> <pre><code>class XmlFile &lt; ActiveRecord::Base end class XmlFileTypeA &lt; XmlFile end class XmlFileTypeB &lt; XmlFile end def create # Leaving this case statement in the controller for simplicity but you probably want to move this to the model case params[:chosen_xml_type] when "file_type_a" @item = XmlFileTypeA.new(params) when "file_type_b" @item = XmlFileTypeB.new(params) else raise "Unknown file type!" etc end end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload