I’ll try renaming it, but I have used this pattern (and names) on many
applications (this test app is an extraction from a working commercial
application of mine, although that uses 4.1.8) and I am just trying to
stub out multiple file uploads in this exercise.
I just re-built my environment – re-installed rvm, ruby 2.1.3, bundler,
rake, rails 4.2.1, and dropped a couple of gems that looked suspicious
– and started completely over with scaffold again. I am getting the
identical problem.
If it’s just the name of the model, I will be frankly surprised, because
each revision of Rails seems to remove reserved words, not add new ones.
While I do that, here’s the actual code – if you can see anything
suspicious, I’d be very grateful.
class Asset < ActiveRecord::Base
belongs_to :project
has_attached_file :blob, :styles => { :large => “800x800”, :medium =>
“400x400>”, :small => “200x200>”}
do_not_validate_attachment_file_type :blob
end
class Project < ActiveRecord::Base
has_many :assets
accepts_nested_attributes_for :assets, reject_if: :all_blank,
allow_destroy: true
end
Rails.application.routes.draw do
resources :projects
resources :assets
end
The pattern I am trying to solve for is upload multiple assets in the
project form (this works perfectly) and edit individual uploaded assets
in their own assets/edit form. This latter part does not work.
This is the form that is failing:
<%= form_for(@asset) do |f| %>
<% if @asset.errors.any? %>
<%= pluralize(@asset.errors.count, “error”) %> prohibited this
asset from being saved:
<ul>
<% @asset.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.label :project_id %>
<%= f.text_field :project_id %>
<%= f.label :blob %>
<%= f.file_field :blob %>
<%= f.submit %>
<% end %>
While this one works just fine:
<%= form_for(@project) do |f| %>
<% if @project.errors.any? %>
<%= pluralize(@project.errors.count, “error”) %> prohibited
this project from being saved:
<ul>
<% @project.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.fields_for :assets do |a| %>
<%= a.label :blob %>
<%= a.file_field :blob, multiple: true, name:
"project[assets_attributes][][blob]" %>
<%- end -%>
<%= f.submit %>
<% end %>
Walter