Configuring Rails 3 + Polymorphic Image model + Paperclip and Amazon S3, No errors, but nothing uploading - ruby-on-rails-3

I suspect the problem lies in the way I am creating the polymorphic image attribute. I am using fields_for in the form.
In this case a user can create a post and add an image using paperclip, storing it with S3.
I am using a polymorphic image model "post_image":
class PostImage < ActiveRecord::Base
belongs_to :imageable, :polymorphic => true
#.merge(PAPERCLIP_OPS)
has_attached_file :image, :styles => { :medium => "200x200>", :thumb => "50x50>" },
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:path => "/:style/:id/:filename",
:bucket => "zounds-dev"
validates_attachment_presence :image
validates_attachment_size :image, :less_than => 5.megabytes
end
Post Model:
class Post < ActiveRecord::Base
has_many :post_images, :as => :imageable, :dependent => :destroy
.
.
.
accepts_nested_attributes_for :post_images, :reject_if => lambda { |t| t[:post_image].nil?}, :allow_destroy => true
end
New Post Form:
=form_for( setup_post(#post,current_user), :html => { :multipart => true}) do |f|
%dl
=f.fields_for :post_images do |ff|
=ff.file_field :image
%dt.field=f.label :name
%dd.field=f.text_field :name
%dt.field=f.label :description
%dd.field=f.text_area :description
=f.fields_for :user do |u|
=render "user_fields", :f => u
=f.fields_for :assignments do |ff|
=ff.check_box :_destroy, {:checked => ff.object.persisted?}, '0','1'
=ff.label :_destroy, ff.object.group.name
=ff.hidden_field :group_id
.action=f.submit "Save Post"
The setup_post helper method used in the Post form_for: (the groups stuff isn't relevant here)
def setup_post(post, current_user)
groups = current_user.groups_as_owner + current_user.groups_as_member
(groups - post.groups).each do |group|
post.assignments.build(:group => group)
end
post.assignments.sort_by {|x| x.group.name }
post_image = post.post_images.build
post
end
Post controller:
def new
#user = User.find(params[:user_id])
# #post = #user.posts.build
#post = Post.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #post }
end
end
def create
#user = current_user
#post = #user.posts.build(params[:post])
.
.
.
end
I suspect the problem is that I am using fields_for for the post_image attribute, but I've looked all over and can't figure out what the proper way to implement a polymorphic nested image attribute is.
I also did the s3sh amazon s3 console thing, and although I couldn't upload an image because I couldn't figure out how to pass in the right image path to the open() function, I connected to S3. My s3.yml file is set up correctly as well.
Thanks yall,
Brian

The issue was with the reject_if in accepts_nested_attributes for the Post model
accepts_nested_attributes_for :post_images, :reject_if => lambda { |t| t[:post_image].nil?}, :allow_destroy => true
commenting it out fixed the issue.

Related

rails has many through where query

My model structure is set up like this,
class Conversation < ActiveRecord::Base
has_many :conversation_statuses, :dependent => :destroy
has_many :users, :through => :conversation_statuses
has_many :messages, :inverse_of => :conversation
accepts_nested_attributes_for :messages
end
class User < ActiveRecord::Base
has_many :conversation_statuses, :dependent => :destroy
has_many :conversations, :through => :conversation_statuses
has_many :messages, :inverse_of => :user
end
class ConversationStatus < ActiveRecord::Base
belongs_to :user
belongs_to :conversation
end
I am trying to create a conversation only if the users don't have a conversation going. I have conversation set up so that more than 2 users can have a conversation. Right now my query is finding conversations including the user ids, but that may include conversations with large groups. My controller query looks like this,
class ConversationsController < ApplicationController
before_filter :set_user_ids_param, :only => :create
def create
#conversation = Conversation.joins(:users)
.where(:users => {:id => params[:conversation][:user_ids]})
.first_or_initialize
#conversation.attributes = conversation_params
if #conversation.save
render :json => { :html => render_new_conversation_form }
return
end
render :status => :bad_request, :json => {
:html => render_conversation_form
}
end
private
def conversation_params
params.require(:conversation).permit([
:user_ids => [],
:messages_attributes => [
:content,
:topic
]
])
end
def set_user_ids_param
return if params[:conversation].blank?
return if params[:conversation].blank? || params[:conversation][:user_ids].blank?
params[:conversation][:user_ids] = params[:conversation][:user_ids].split(',')
params[:conversation][:user_ids].push(current_user.id)
end
def render_new_conversation_form
render_to_string({
:partial => 'conversations/form',
:locals => {
:conversation => #conversation
}
})
end
def render_conversation_form
render_to_string({
:partial => 'conversations/form',
:locals => {
:conversation => #conversation
}
})
end
end
and my form looks like this
= simple_form_for conversation, :html => {:class => 'conversation-form'} do |form|
= form.input :user_ids, :as => :hidden, :input_html => {:class => 'user-ids'}
= form.simple_fields_for :messages do |message_fields|
= render 'messages/fields', :message_fields => message_fields
%button.button.radius.submit.no-margin
Send Message
%button.button.secondary.radius.cancel.no-margin
Cancel
Any ideas on the best way to handle this?
The method that will resolve your problem should be in class ConversationStatus, which will give you the conversation_id of the Conversation, if it exists.
You'll need two sets:
containing_all_users are the conversations that contain all the users in users_id
with_extra_users are the conversations that have more users than those in users_id
The conversation (if exists), is the result of the subtraction of those sets.
class ConversationStatus < ActiveRecord::Base
belongs_to :user
belongs_to :conversation
def self.find_between(user_ids)
containing_all_users = group(:conversation_id)
.where(user_id: user_ids)
.having(['COUNT(user_id) = ?', user_ids.length])
.pluck(:conversation_id)
with_extra_users = group(:conversation_id)
.having(['COUNT(user_id) > ?', user_ids.length])
.pluck(:conversation_id)
(containing_all_users - with_extra_users).first
end
end
You'll get the conversation from the Conversation model. You should set the attributes on this method too:
class Conversation < ActiveRecord::Base
has_many :conversation_statuses, dependent: :destroy
has_many :users, through: :conversation_statuses
def self.find_between(user_ids, attributes = {})
conversation_id = ConversationStatus.find_between(user_ids)
Conversation.where(id: conversation_id).first_or_initialize(attributes)
end
end
On the controller you will call it like this:
#conversation = Conversation.find_between(params[:conversation][:user_ids],
conversation_params)

Nested Form: Can’t populate join table between parent and child if child exists / Couldn't find Child with ID=1 for ParentChildJoin with ID=

I cleaned up my code, it looks much nicer now, but still doesn’t work. It starts to be a pain…
I just can’t save a parent with an existing child in nested form with parent has_many childs :through joinmodel.
In my case a Project has_many contributing Teachers and many contributing Pupils, both are Join-Models to Users. A Project has_many Schools as well.
(May be I should better name the models Teacherize and Pupilize or ProjectTeacher and ProjectPupil.)
As long as all records are new it all works fine. As soon as I want to connect an existing User as new Teacher of a new Project I get the following error:
Couldn't find User with ID=1 for Teacher with ID=
(1 is the correct user ID)
The problem should be somewhere her in my helper to setup empty form fields:
At least I guess so...
module ProjectsHelper
def setup_project(project)
if project.teachers.length <= 0 # usually there is just one teacher, so add one if there isn't one
teacher = project.teachers.build(:role_in_project => 'master')
if user_signed_in?
#teacher = project.teachers.new(:role_in_project => 'master', :user => current_user)
teacher.user = current_user # associate first teacher with current_user
else
#teacher = project.teachers.build
teacher.user = User.new # associate first teacher with a new user instance
end
end
if project.project_schools.length <= 0 # usually there is just one school, so add one if there isn't one
project_school = project.project_schools.build
project_school.school = School.new
end
if project.pupils.length < 3 # There can be up to 3 people, so add a blank fieldset as long as there are less than 3
pupil = project.pupils.build
pupil.user = User.new
end
project
end
end
These are my params received:
{"utf8"=>"✓", "authenticity_token"=>"uCCMk/s3SpDfR7+fXcsCOHPvfvivBQv8pVFVhdh6iro=",
"project"=>{
"teachers_attributes"=>{
"0"=>{
"id"=>"",
"user_attributes"=>{
"id"=>"1",
"gender"=>"male",
"title"=>"",
"firstname"=>"Firstname1",
"name"=>"Lastname1",
"faculty"=>"",
"fon"=>"",
"fax"=>""}
}
},
"id"=>"",
"title"=>"First Project",
"description"=>"This is a foo bar project!",
"presentation_type"=>"experimentell",
"note"=>""
},
"commit"=>"Register Project",
"action"=>"create",
"controller"=>"projects"
}
The case isn’t too abstract; it has to be possible to achieve it.
It’s just to connect a new parent record with an existing child record!
In this article, which is very good, exactly the case is explaint:
http://rubysource.com/complex-rails-forms-with-nested-attributes
# app/helpers/form_helper
module FormHelper
def setup_user(user)
user.address ||= Address.new
(Interest.all - user.interests).each do |interest|
user.interest_users.build(:interest => interest)
end
user.interest_users.sort_by! {|x| x.interest.name }
user/tmp/clean-controllers.md.html
end
end
There the interest is existing and gets connected through a new record in interest_uesers.
Why do I get the error when trying to do the same thing?
project.teachers.build(:user => current_user)
I studied several articles and casts, but none of them connect existing childs.
http://railscasts.com/episodes/196-nested-model-form-revised
http://apidock.com/rails/ActionView/Helpers/FormHelper/fields_for
http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html
Rails 3.1+ Nested Forms Issue: Can't mass-assign protected attributes
Trying to use accepts_nested_attributes_for and has_and_belongs_to_many but the join table is not being populated
Quote: “accepts_nested_fields_for is used to create and modify related objects in a form. It can be used to populate join table, which is kind of what you're trying to do. However, using accepts_nested_fields_for to populate the join table is impossible with a HABTM relationship.”
That’s what I wanna do! Populate the join table!
It starts to be frustrating and I’d be glad to get some help!
My Models
class Project < ActiveRecord::Base
attr_accessible :title, :description, :presentation_type, :note,
:project_schools_attributes, :schools_attributes, :teachers_attributes, :pupils_attributes,
:users_attributes
validates_presence_of :title
validates_presence_of :description
validates_presence_of :presentation_type
has_many :project_schools, :dependent => :destroy
accepts_nested_attributes_for :project_schools
has_many :schools, :through => :project_schools
#accepts_nested_attributes_for :schools, :reject_if => :all_blank
has_many :pupils, :dependent => :destroy
accepts_nested_attributes_for :pupils, :reject_if => :all_blank
has_many :users, :through => :pupils
has_many :teachers, :dependent => :destroy
accepts_nested_attributes_for :teachers, :reject_if => :all_blank
has_many :users, :through => :teachers
#accepts_nested_attributes_for :users, :reject_if => :all_blank
end
class ProjectSchool < ActiveRecord::Base
attr_accessible :role_in_project, :comment,
:school_attributes, :school_id, :project_id
belongs_to :school
accepts_nested_attributes_for :school
belongs_to :project
end
class School < ActiveRecord::Base
attr_accessible :email, :fax, :fon, :name, :place, :street, :type_of_school, :www, :zip
has_many :project_schools
has_many :projects, :through => :project_schools
has_many :users # in real live they are named teachers and pupils but in this case the association goes directly to a user_id, not to teacher/pupil model
validates_presence_of :name, :type_of_school, :street, :place, :zip, :fon
validates :email, :format => { :with => /\A([^#\s]+)#((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :on => :create }, :allow_blank => true
end
class Teacher < ActiveRecord::Base
attr_accessible :role_in_project, :user, :user_attributes, :project_id, :user_id
belongs_to :project
belongs_to :user
accepts_nested_attributes_for :user
serialize :role_in_project
end
class Pupil < ActiveRecord::Base
attr_accessible :classname, :user_attributes #, :project_id, :user_id
belongs_to :project
belongs_to :user
accepts_nested_attributes_for :user
end
class User < ActiveRecord::Base
serialize :roles
belongs_to :school
has_many :teachers
has_many :pupils
has_many :projects, :through => :teachers
has_many :projects, :through => :pupils
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :gender, :firstname, :name, :street, :place, :title, :faculty, :assignment,
:classname, :zip, :fon, :fax, :school_id, :roles,
:password, :added_by_user_id, :password_confirmation, :remember_me
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable#, :validatable
after_initialize :init
def init
# the default guest user
self.roles ||= ['default'] #will set the default value only if it's nil
end
end
My controller
class ProjectsController < ApplicationController
require 'axlsx'
before_filter :load_page, only: [:show, :index, :destroy]
def new
#project = Project.new
end
def create
#project = Project.new(params[:project])
respond_to do |format|
if #project.save
sign_in(:user, #project.teachers[0].user) unless user_signed_in?
# TODO: send mail
# save as excel file in dropbox
save_in_dropbox(#project)
format.html { redirect_to #project, notice: t('project.was_created') }
else
logger.debug #project.errors.inspect
format.html { render action: "new" }
end
end
end
end
projects/_form.html.haml
%h1
= t('project.register_headline')
= simple_form_for( setup_project(#project), :html => {:class => 'form-horizontal'} )do |f|
= f.error_notification
#teachers-wrapper.well
%span.jumpanchor#lehrkraft_anchor
%fieldset.form-inputs
= f.simple_fields_for :teachers do |teacher|
= render "teacher_fields", :f => teacher
.school-wrapper.well
%span.jumpanchor#schule_anchor
%h2
Informationen zur Schule
%fieldset.form-inputs
= f.simple_fields_for :project_schools do |project_school|
= render "school_fields", :f => project_school
.project-wrapper.well
%span.jumpanchor#projekt_anchor
%h2
Informationen zum Projekt der Schüler
%fieldset.form-inputs
= f.hidden_field :id
= f.input :title, :input_html => { :class => 'span6' }
= f.input :description, :input_html => { :class => 'span6', rows: 5 }
= f.input :presentation_type, collection: ['theoretisch', 'experimentell'], as: :radio_buttons, :class => 'controls-row', :input_html => { :class => 'inline' }
.clearfix
= f.input :note, :input_html => { :class => 'span6', rows: 3 }
.pupils-wrapper.well
%span.jumpanchor#schuler_anchor
%fieldset.form-inputs
= f.simple_fields_for :pupils do |pupil|
= render "pupil_fields", :f => pupil
projects/_teacher_fields.html.haml
= f.simple_fields_for :user do |user|
=# render "teacher_user_fields", :f => user
%h2
Betreuende Lehrkraft
- if user_signed_in?
= user.input :email, :disabled => true, :input_html => {:class => 'email_validation'}
- else
= user.input :email, :autofocus => true, :input_html => {:class => 'email_validation'}, :hint => 'Dies muß Ihre eigene E-Mailadresse sein!'
.details
=# user.hidden_field :id
= user.input :id
= user.input :gender, collection: [:female, :male]
= user.input :title
= user.input :firstname
= user.input :name
= user.input :faculty
= user.input :fon
= user.input :fax
It is Rails 3.2 with Ruby 1.9.3

Rails 3.0 associated model with download 'save to' image

My Rails 3.0.3 application has a scaffold 'month' which has a link where the user can download an image using 'save to'.
Now I need to make an association where the month model belongs_to the wallpaper model.
Routes:
root :to => 'inicio#index'
resources :wallpapers do
resources :months
end
# the route that works with no association
# match 'download/:id' => 'months#download', :as => :download
# the route I tried
match 'wallpapers/:id/months/:id' => 'months#download', :as => :download
Month model:
class Month < ActiveRecord::Base
belongs_to :wallpaper
has_attached_file :wallpaper_picture, :styles => {
:default => { :geometry => '530x330', :quality => 80, :format => 'jpg'}
}
end
Wallpaper model with friendlyid:
class Wallpaper < ActiveRecord::Base
has_many :months, :dependent => :destroy
extend FriendlyId
friendly_id :title, :use => :slugged
end
In months_controller I made the download method, this method works with no association:
class MonthsController < InheritedResources::Base
belongs_to :wallpaper, :finder => :find_by_slug!
def download
#wallpaper = Wallpaper.find(params[:wallpaper_id])
#month = #wallpaper.month.find(params[:id])
send_file #month.wallpaper_picture.path,
:filename => #month.wallpaper_picture_file_name,
:type => #month.wallpaper_picture_content_type,
:disposition => 'attachment'
end
end
View months/index
- #months.each do |month|
= link_to image_tag(month.wallpaper_picture(:default)), wallpaper_month_path(month.wallpaper, month)
I've tried changing in the months_controller the download method, but it is wrong:
#months = Wallpaper.month.conditions({:person_id => some_id})
Here is how I got it
Routes
resources :wallpapers do
resources :months
end
match 'wallpaper/:wallpaper_id/download/:id' => 'months#download', :as => :download
In the routes I must pass the :wallpaper_id (has_many :months),
the :id is the id of the current controller (belongs_to :wallpaper)
'download' will be the name of the path used in the view 'download_path'
In this path I must pass the foreign key and the id
View months/index
- #months.each do |month|
= link_to 'Download Picture', download_path(month.wallpaper_id, month.id)
In months_controller the download method will receive those parameters and pass the associated image to the send_file method.
def download
#wallpaper = Wallpaper.find(params[:wallpaper_id])
#month = #wallpaper.months.find(params[:id])
send_file #month.wallpaper_picture.path,
:filename => #month.wallpaper_picture_file_name,
:type => #month.wallpaper_picture_content_type,
:disposition => 'attachment'
end
PD: if send_file fails in Production, change it to send_data or
comment out this line in config/production.rb
config.action_dispatch.x_sendfile_header = "X-Sendfile"
send_file just sends an empty file

Update nested attributes before saving to database

Long time reader of Stackoverflow but have never found myself in a position to ask a question (that hasn't already been answered). I guess there's a first time for everything so here it goes...
System Info:
Ruby Version = 1.8.7
Rails Version = 3.2.2
Situation:
We have an application with a user registration system in place. In order to hook up and populate all of our tables correctly, we are utilizing Complex/Nested Forms within the registration view. I actually have the nested forms working perfectly, everything is being populated as it should, its awesome really.
Here is the problem: I need to set one of the value of one of the nested attributes AFTER the form post but BEFORE the records are saved.
Here is a quick example so you can see what I'm talking about a little bit better:
A user registers with our site. When they register a record is created in the Users data table. Each user is also classified as a team_mate (join table) and assigned to their very own individual team (at first). But, a 'team' (table) also has an 'alias' field in it which, on the initial creation of the user we would like to set to the users first name (without having to have them enter their first name into an 'alias' field on the form).
So, I guess the question would be: How to I manually set the value of a nested attribute after the form post and before the records are saved to the database?
A (simplistic) example of the table schema looks is as follows:
Users (id, first_name, last_name, created_at, updated_at)
Team_mates(id, user_id, team_id, created_at, updated_at) - join table
Teams(id, alias, created_at, updated_at)
Models:
User.rb
class User < ActiveRecord::Base
has_many :team_mates, :dependent => :destroy
has_many :teams, :through => :team_mates, :foreign_key => :team_id
accepts_nested_attributes_for :team_mates, :allow_destroy => true
before_save :set_defaults
private
def set_defaults
#want to set :users => :team_mates_attributes => :team_attributes => :alias to #user.first_name here
# Would prefer to handle this here instead of in the controller.
end
end
Team.rb
class Team < ActiveRecord::Base
has_many :team_mates, :dependent => :destroy
has_many :users, :through => :team_mates, :foreign_key => :user_id
end
Team_mate.rb
class TeamMate < ActiveRecord::Base
belongs_to :user
belongs_to :team
accepts_nested_attributes_for :team, :allow_destroy => true
end
Controller
Users_controller.rb
class UsersController < ApplicationController
def new
#user = User.new
#user.emails.build(:is_default_email => 1)
#user.build_login
#user.team_mates.build.build_team(:alias => 'Clinton444', :created_at => Time.new, :updated_at => Time.new)
respond_to do |format|
format.html
format.json { render :json => #match }
end
end
def create
#user = User.new(params[:user])
#user.attributes = ({ "user" => { "team_mates" => { "team" => { "alias" => #user.first_name } } } }) #--this doesn't work...
#user.attributes = ({ :user => { :team_mates => { :team => { :alias => #user.first_name } } } }) #--neither does this...
respond_to do |format|
if #user.save
format.html { redirect_to(#user, :notice => 'User was successfully created.') }
format.json { render :json => #user, :status => :created, :location => #user }
else
format.html { render :action => "new" }
format.json { render :json => #user.errors, :status => :unprocessable_entity }
end
end
end
View
new.html.haml
= form_for(#user, :html => {:class => 'form-horizontal'}) do |f|
- if #user.errors.any?
.alert
%h2
= pluralize(#user.errors.count, "error")
prohibited this post from being saved:
%ul
- #user.errors.full_messages.each do |msg|
%li
= msg
%fieldset
.control-group
= f.label :first_name, :class => "control-label"
.controls
=f.text_field :first_name, :class => "span8"
.control-group
= f.label :last_name, :class => "control-label"
.controls
=f.text_field :last_name, :class => "span8"
= f.fields_for :emails do |e|
=e.hidden_field :is_default_email, :class => "span8"
.control-group
= e.label :email, :class => "control-label"
.controls
=e.text_field :email, :class => "span8"
= f.fields_for :team_mates do |tm|
= tm.fields_for :team do |t|
=t.hidden_field :alias, :class => "span8"
=t.hidden_field :created_at, :class => "span8"
=t.hidden_field :updated_at, :class => "span8"
= f.fields_for :login do |e|
.control-group
= e.label :user_login, :class => "control-label"
.controls
=e.text_field :user_login, :class => "span8"
.control-group
= e.label :password_encrypted, :class => "control-label"
.controls
=e.text_field :password_encrypted, :class => "span8"
.control-group
.controls
=f.submit :class => 'btn btn-primary btn-medium'
And finally
Rails server output on form post
Parameters: {"user"=>{"team_mates_attributes"=>{"0"=>{"team_attributes"=>{"created_at"=>"Wed Jun 06 09:52:19 -0600 2012", "alias"=>"asfs444", "updated_at"=>"Wed Jun 06 09:52:19 -0600 2012"}}}, "first_name"=>"lkjlkjlsdfslkjeowir", "last_name"=>"ouisodifuoixv", "emails_attributes"=>{"0"=>{"is_default_email"=>"1", "email"=>"lpisfsopf#psflsjdk.com"}}, "login_attributes"=>{"user_login"=>"lkjsdfooiusfd", "password_encrypted"=>"[FILTERED]"}}, "utf8"=>"✓", "commit"=>"Create User", "authenticity_token"=>"CQLQ93/0VlncSzMlmtLPHgaVrrvjuHFN+lN6CYCsiR8="}
After looking at the models you might be wondering where emails/logins are coming from. They're built within the model on our system, but are not really part of this question so I omitted the code for them. They are working, so the problem isn't on that side.
Check http://archives.ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes
To support both the creation of new objects and the editing of
existing ones we have to use an array of hashes for one-to-many
associations or a single hash for one-to-one associations. If no :id
property exists then it is assumed to represent a nested model to
create.
Not 100% sure.. I haven't used\tested it before, but this should give you an idea
#user.teams.each do |team|
team.team_mates do |team_mate|
# To edit existing
team_mate.team_attributes = [ { :id => team.id, :alias => #user.first_name } ]
# To create new
team_mate.team_attributes = [ { :alias => #user.first_name } ]
team_mate.save
end
end

Multiple (n) identical nested forms generated square-times(n*n) when validation fails

User has two addresses shipping(:address_type=0) and billing(:address_type=1)
User form with 2 classic nested forms for each address type are generated square times every submit and failed validation.
Models:
class User < ActiveRecord::Base
has_many :addresses, :dependent => :destroy
accepts_nested_attributes_for :addresses
validates_associated :addresses
end
class Address < ActiveRecord::Base
belongs_to :user
validates :user, :address_type, :first_name, :last_name, :street
end
Controller
class UsersController < ApplicationController
public
def new
#user = User.new
#shipping_address = #user.addresses.build({:address_type => 0})
#billing_address = #user.addresses.build({:address_type => 1})
end
def create
#user = User.new(params[:user])
if #user.save
#fine
else
render => :new
end
end
Uncomplete Form
=form_for #user, :html => { :multipart => true } do |ff|
=ff.fields_for :addresses, #shipping_address do |f|
=f.hidden_field :address_type, :value => 0
=ff.fields_for :addresses, #billing_address do |f|
=f.hidden_field :address_type, :value => 1
=ff.submit
The form should look like this:
=form_for #user, :html => { :multipart => true } do |ff|
=ff.fields_for :addresses do |f|
Nothing else.
Addressess is already a collection, so you should have just one rendering of it.
Also that ":addresses, #shipping_address" makes it to render addresses AND shipping address, even if it's included in #user.addresses.
The addressess built in new action will show there because they are in the addresses collection.
EDIT:
If you need only these two addresses, you can sort it and pass it to fields_for directly:
=form_for #user, :html => { :multipart => true } do |ff|
=ff.fields_for ff.object.addresses.sort{|a,b| a.address_type <=> b.address_type } do |f|
That should do it.
Surprised? I guess not but I was. I found it am I correct? And its stupid and simple.
There is no #shipping_address nor #billing_address when validation fails and rendering the new action (the form) again. But #user has already 2 addresses builded and nested form behave correctly to render each twice for first time failed validation.
def create
#user = User.new(params[:user])
if #user.save
#fine
else
#user.addresses.clear
#user_address = #user.addresses.build({:address_type => 0})
#user_address.attributes = params[:user][:addresses_attributes]["0"]
#billing_address = #user.addresses.build({:address_type => 1})
#billing_address.attributes = params[:user][:addresses_attributes]["1"]
render => :new
end
end