On Oct 26, 2011, at 12:17 PM, Javier Q. wrote:
_children.html.erb
<%= f.semantic_fields_for “user[children][]”, user do |children| %>
…
<% end %>
and I get this
undefined local variable or method `user’
It’s maybe something I’m not seeing, I’m starting with rails and I hope someone
can help me
Here’s an extraction from a site that’s using Ryan’s plug-in (a person
can have N images):
#people_controller.rb
…
def new
@person = Person.new
@person.images.build
end
…
#person.rb
class Person < ActiveRecord::Base
attr_accessible :images_attributes, :name, :sort_name, :birth, :death,
:nationality, :historical_period, :short_description, :bio
has_many :images, :as => :attachable, :dependent => :destroy
accepts_nested_attributes_for :images, :reject_if => lambda { |a|
a[:file].blank? && a[:file_uid].blank? && a[:file_url].blank? &&
a[:name].blank? }, :allow_destroy => true
validates :name, :presence => true
validates :nationality, :presence => true
validates :historical_period, :presence => true
end
#image.rb
class Image < ActiveRecord::Base
attr_accessible :name, :attachable_type, :attachable_id, :file_name,
:file_url, :file_uid, :file, :retained_file, :remove_file, :created_at,
:file_height, :updated_at, :file_width
image_accessor :file
belongs_to :attachable, :polymorphic => true
def label
name.blank? ? file_name : name
end
end
#people/_form.html.erb
<%= nested_form_for @person, :url => person_path(@person, :page =>
params[:page]), :html => {:multipart => true} do |f| %>
<%= f.error_messages %>
…
<%= f.fields_for :images %>
<%= f.link_to_add "Add an image", :images %>
<%= f.submit %>
<% end %>
#people/_image_fields.html.erb (this name is critical to success)
<%- if f.object.file_uid -%>
<%= link_to( image_tag( f.object.file.thumb('60x60#').url, :class =>
'border', :size => '60x60' ), f.object.file.url, :target => '_blank')
%>
<%= f.object.file_width %> <%=
f.object.file_height %>
<%= f.link_to_remove "Delete", :class => 'delete' %>
<%- else -%>
<%= image_tag( 'thumb_new.jpg', :class => 'border', :size => '60x60'
) %>
<%- end -%>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :file, "Upload" %>
<%= f.file_field :file %>
<%= f.hidden_field :retained_file %>
<%= f.label :file_url, "Upload From URL" %>
<%= f.text_field :file_url %>
That is quite literally all there is to it, except to follow the rest of
the install directions on the Github page.
Walter