Hi,
Could you look at some code I created? I don’t know if it’s good… or
if it could be better.
Especially the controller. I couldn’t find a way to let the
declaration_payers be automatically saved.
declarations_controller.rb:
def create
@declaration = Declaration.new(params[:declaration])
unless @declaration.save
@users = User.find(:all)
render :action => 'new'
return false
end
@declarations_payers = params[:declarations_payers]
@additional_persons = params[:additional_persons]
#If there are payers, add them
if @declarations_payers
@declarations_payers.each_value { |user_id|
#Add new object
@declarations_payer = DeclarationsPayer.new
#Set user_id variable (from post)
@declarations_payer.user_id = user_id
#Add aditional persons (from post)
@declarations_payer.additional_persons =
@additional_persons[user_id]
#Add the object to the declaration object
@declaration.declarations_payers << @declarations_payer
}
end
flash[:notice] = 'Declaration was successfully created.'
redirect_to :action => 'list'
end
_form.rhtml:
<%= error_messages_for ‘declaration’ %>
Amount
<%= text_field 'declaration', 'amount' %>
Description
<%= text_field 'declaration', 'description' %>
<% unless @users.nil? %>
<% for user in @users %> <%= user.fullname %>
<%= check_box_tag 'declarations_payers['+user.id.to_s+']',
user.id, @declaration.users.include?(user) %>
met
<%= text_field_tag ‘additional_persons[’+user.id.to_s+’]’,
@declaration_payers[user.id], ‘size’ => 1 %>
additionele personen
<br />
<% end %>
<% end %>Thanks in advance!