Ruby on Rails: create records for multiple models with one form and one submit

Posted by notblakeshelton on Stack Overflow See other posts from Stack Overflow or by notblakeshelton
Published on 2013-06-26T21:33:07Z Indexed on 2013/06/26 22:21 UTC
Read the original article Hit count: 132

Filed under:
|
|

I have a 3 models: quote, customer, and item. Each quote has one customer and one item. I would like to create a new quote, a new customer, and a new item in their respective tables when I press the submit button. I have looked at other questions and railscasts and either they don't work for my situation or I don't know how to implement them. I also want my index page to be the page where I can create everything.

quote.rb

class Quote < ActiveRecord::Base
  attr_accessible :quote_number
  has_one :customer
  has_one :item
end

customer.rb

class Customer < ActiveRecord::Base
  #unsure of what to put here
  #a customer can have multiple quotes, so would i use:
  has_many :quotes #<----?
end

item.rb

class Item < ActiveRecord::Base
  #also unsure about this
  #each item can also be in multiple quotes

quotes_controller.rb

class QuotesController < ApplicationController
  def index
    @quote = Quote.new
    @customer = Customer.new
    @item = item.new
  end

  def create
    @quote = Quote.new(params[:quote])
    @quote.save
    @customer = Customer.new(params[:customer])
    @customer.save
    @item = Item.new(params[:item])
    @item.save
  end
end

items_controller.rb

class ItemsController < ApplicationController
  def index
  end

  def new
    @item = Item.new
  end

  def create
    @item = Item.new(params[:item])
    @item.save
  end
end

customers_controller.rb

class CustomersController < ApplicationController
  def index
  end

  def new
    @customer = Customer.new
  end

  def create
    @customer = Customer.new(params[:customer])
    @customer.save
  end
end

quotes/index.html.erb

<%= form_for @quote do |f| %>
  <%= f.fields_for @customer do |builder| %>
    <%= label_tag :firstname %>
    <%= builder.text_field :firstname %>
    <%= label_tag :lastname %>
    <%= builder.text_field :lastname %>
  <% end %>
  <%= f.fields_for @item do |builder| %>
    <%= label_tag :name %>
    <%= builder.text_field :name %>
    <%= label_tag :description %>
    <%= builder.text_field :description %>
  <% end %>
  <%= label_tag :quote_number %>
  <%= f.text_field :quote_number %>
  <%= f.submit %>
<% end %>

When I try submitting that I get an error:

Can't mass-assign protected attributes: item, customer

So to try and fix it I updated the attr_accessible in quote.rb to include :item, :customer but then I get this error:

Item(#) expected, got ActiveSupport::HashWithIndifferentAccess(#)

Any help would be greatly appreciated.

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about forms