Ruby on Rails Sunday, May 3, 2015

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? %>
<div id="error_explanation">
<h2><%= pluralize(@asset.errors.count, "error") %> prohibited this asset from being saved:</h2>

<ul>
<% @asset.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= f.label :project_id %>
<%= f.text_field :project_id %>
</div>
<div class="field">
<%= f.label :blob %>
<%= f.file_field :blob %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>

While this one works just fine:

<%= form_for(@project) do |f| %>
<% if @project.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@project.errors.count, "error") %> prohibited this project from being saved:</h2>

<ul>
<% @project.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<%= f.fields_for :assets do |a| %>
<div class="field">
<%= a.label :blob %>
<%= a.file_field :blob, multiple: true, name: "project[assets_attributes][][blob]" %>
</div>
<%- end -%>
<div class="actions">
<%= f.submit %>
</div>
<% end %>


Walter

On May 3, 2015, at 10:13 AM, Colin Law <clanlaw@gmail.com> wrote:

> On 3 May 2015 at 14:45, Walter Lee Davis <waltd@wdstudio.com> wrote:
>> Started PATCH "/assets/1" for ::1 at 2015-05-02 15:32:49 -0400
>
> Have you got a model called Asset? I suspect that might cause
> confusion with assets (js, css etc).
> Is it only with assets that you get the problem?
>
> Colin
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLsfyN983fSdOybQxazfRQ1LtgVgAqL0SrpOtHhJFfXWVA%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/21F8C8FF-4CF9-4E51-9B59-E645E0A92535%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment