开发者

Rails, many instances for one model

开发者 https://www.devze.com 2023-04-07 14:06 出处:网络
How can I create many instances of one model in one form? In my case user must be able to upload many files with开发者_C百科 descriptions in one time.If there is an association between users and files

How can I create many instances of one model in one form? In my case user must be able to upload many files with开发者_C百科 descriptions in one time.


If there is an association between users and files in your models, you can use nested attributes to build your form with attributes from each model. Check out http://ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes to get started

Your form would look something like this:

<%= form_for @user do |user_form| %>
  First name: <%= user_form.text_field :first_name %>
  Last name : <%= user_form.text_field :last_name %>

  <%= fields_for @user.files do |file_fields| %>
    <%= file_fields.text_field :name %>
  <% end %>

  <%= f.submit %>
<% end %>
0

精彩评论

暂无评论...
验证码 换一张
取 消