I am displaying an invoice, to which I want to add 'fly' products. I want to do this using a drop down box for the products along with an add button. Once a product has been added to the invoice I want to be able to click a delete button to remove it.
I currently have this working through the console, but am not sure how to do this on the front end.
How it is set up::
User Model:
class User < ActiveRecord::Base
....
has_many :invoices
....
end
Invoice Model:
class Invoice < ActiveRecord::Base
attr_accessible :active
validates :user_id, presence: true
belongs_to :user
has_many :categorizations
has_many :flies, through: :categorizations
end
Invoice migration:
class CreateInvoices < ActiveRecord::Migration
def change
create_table :invoices do |t|
t.boolean :active
t.integer :user_id
t.timestamps
end
add_index :invoices, :user_id
end
end
Categorization Model:
class Categorization < ActiveRecord::Base
attr_accessible :fly_id, :user_id
belongs_to :invoice
belongs_to :fly
end
Categorization migration:
class CreateCategorizations < ActiveRecord::Migration
def change
create_table :categorizations do |t|
t.integer :user_id
t.integer :fly_id
t.timestamps
add_index :categorizations, :user_id
add_index :categorizations, :fly_id
end
end
end
Fly Model:
class Fly < ActiveRecord::Base
attr_accessible :description, :name
validates :description, :name, presence: true
has_many :categorizations
has_many :invoices, through: :categorizations
end
Fly migration:
class CreateFlies < ActiveRecord::Migration
def change
create_table :flies do |t|
t.string :name
t.string :description
t.timestamps
end
end
end
Show invoice view:
<h3>Invoice</h3>
<p>User Name:
<%= @invoice.user.name %></p>
<p>
Invoice ID:
<%= @invoice.id %></p>
<p>
Invoice Active?:
<%= check_box_tag 'admin', '1', @invoice.active, :disabled => true %></p>
<p>Email:
<%= @invoice.user.email if @invoice.user.email %></p>
<table class="table table-condensed">
<thead>
<tr>
<th>Invoice Flies</th>
</thead>
<tbody>
<% @invoice.flies.each do |fly| %>
<tr>
<td><%= fly.name %></td>
</tr>
<% end %>
</tbody>
</table>
<%= simple_form_for(@categorization) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
#this is where I want to add my 'add product to invoice' functionality
<%= f.submit "Add Fly to Invoice", class: "btn btn-large btn-primary" %>
<% end %>
<%= button_to "Mark as Sent", {:controller => :invoices, :action => :activate, :id => @invoice.id }, {:method => :post } %>
<%= button_to "Mark as not sent", {:controller => :invoices, :action => :deactivate, :id => @invoice.id }, {:method => :post } %>
<br><br>
<%= link_to "Back to list of invoices", invoices_path %>