Ignoring a model with all blank fields in Ruby on Rails
- by aguynamedloren
I am trying to create multiple items (each with a name value and a content value) in a single form. The code I have is functioning, but I cannot figure out how to ignore items that are blank. Here's the code:
#item.rb
class Item < ActiveRecord::Base
attr_accessible :name, :content
validates_presence_of :name, :content
end
#items_controller.rb
class ItemsController < ApplicationController
def new
@items = Array.new(3){ Item.new }
end
def create
@items = params[:items].values.collect{|item|Item.new(item)}
if @items.each(&:save!)
flash[:notice] = "Successfully created item."
redirect_to root_url
else
render :action => 'new'
end
end
#new.html.erb
<% form_tag :action => 'create' do %>
<%@items.each_with_index do |item, index| %>
<% fields_for "items[#{index}]", item do |f| %>
<p>
Name: <%= f.text_field :name %>
Content: <%= f.text_field :content %>
</p>
<% end %>
<% end %>
<%= submit_tag %>
<% end %>
This code works when all fields for all items are filled out in the form, but fails if any fields are left blank (due to validations). The goal is that 1 or 2 items could be saved, even if others are left blank.
I'm sure there is a simple solution to this, but I've been tinkering for hours with no avail. Any help is appreciated!