0
我正在顯示一張發票,我想要添加「飛」產品。我想用產品的下拉框和一個添加按鈕來做到這一點。一旦產品已添加到發票中,我希望能夠點擊刪除按鈕將其刪除。HABTM - 添加和刪除按鈕(在顯示視圖中)
我目前有這個工作通過控制檯,但我不知道如何做到這一點在前端。
它是如何設置::
用戶模型:
class User < ActiveRecord::Base
....
has_many :invoices
....
end
發票型號:
class Invoice < ActiveRecord::Base
attr_accessible :active
validates :user_id, presence: true
belongs_to :user
has_many :categorizations
has_many :flies, through: :categorizations
end
發票遷移:
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
分類模型:
class Categorization < ActiveRecord::Base
attr_accessible :fly_id, :user_id
belongs_to :invoice
belongs_to :fly
end
分類遷移:
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
飛型號:
class Fly < ActiveRecord::Base
attr_accessible :description, :name
validates :description, :name, presence: true
has_many :categorizations
has_many :invoices, through: :categorizations
end
粉煤灰遷移:
class CreateFlies < ActiveRecord::Migration
def change
create_table :flies do |t|
t.string :name
t.string :description
t.timestamps
end
end
end
顯示發票視圖:
<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 %>