Created
December 15, 2011 06:55
-
-
Save Justinwceo/1480119 to your computer and use it in GitHub Desktop.
Rendering error messages for form_tag that creates one model multiple times like form_for?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= f.text_field :name %> | |
<%= f.text_field :price %> | |
<%= f.text_field :product_store %> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= form_tag create_multiple_products_path, :method => :post do %> | |
<%= error_messages_for @product %> | |
# the :purchase_date and :send_to_data are merged into all 5 Products. | |
<%= date_select("product", "purchase_date") %> | |
<%= label_tag :send_to_data, 'Send to Data?' %> | |
<%= radio_button("product", :send_to_data, 1) %> | |
<%= radio_button("product", :send_to_data, 0) %> | |
<% @products.each_with_index do |product, index| %> | |
<%= fields_for "products[#{index}]", product do |p| %> | |
<%= render "fields", :f => p %> | |
<% end %> | |
<% end %> | |
<%= submit_tag "Done" %> | |
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Product < ActiveRecord::Base | |
attr_accessible :price, :name, :purchase_date, :product_store, :send_to_data | |
belongs_to :user | |
belongs_to :store | |
attr_reader :product_store | |
validates_inclusion_of :send_to_data, :in => [true, false] | |
validates_presence_of :name, :price, :store_id, :user_id | |
validates_numericality_of :price | |
def product_store=(id) | |
self.store_id = id | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ProductsController < ApplicationController | |
def new | |
@products = Array.new(5) { Product.new } | |
end | |
def create_multiple | |
@products = current_user.products.create(params[:products].map { |_k, up| up.merge params[:product] }) | |
respond_to do |format| | |
if @products.all?(&:valid?) | |
@products.each(&:save!) | |
format.html { redirect_to :back, :notice => "Success!" } | |
format.xml { render :xml => @product, :status => :created, :location => @product } | |
else | |
format.html { redirect_to :back, :notice => "An error occured, please try again." } | |
format.xml { render :xml => @product.errors, :status => :unprocessable_entity } | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment