I created a view that show all my invoices products and also created a link that remove an invoice product from a div and it will be updated so everytime that I remove everything will be in the same page.
Here is the table
invoices
|id| |name|
1 ABC
2 DEF
invoice_products
|id| |invoice_id| |word|
1 1 AAAA
2 1 BBBB
3 2 CCCC
4 2 DDDD
Here is the controller:
def show
@invoice= Invoice.find(params[:id])
end
def destroy_job
@job = InvoiceProduct.find(params[:id])
@invoice = @job.invoice
@job.destroy()
render :partial=>"finance_management/invoice/partials/new_subjects" }
end
Here is the model
class Invoice < ActiveRecord::Base
has_many :invoice_products
end
class InvoiceProduct < ActiveRecord::Base
belongs_to :invoice
end
Here is the view: "show.html.erb"
<%= @invoice.id %>
<div id="table"%>
<% @invoice.invoice_products.each do |i| %>
<%= i.name %>
<%= i.word %>
<%=link_to_remote(image_tag("image.png"), :update => "table",:url => { :controller=>'finance_management/invoice_product',:action => 'destroy_job',:id=>i.id } )%>
<% end %>
</div>
The log:
ActionView::TemplateError (undefined method `invoice_products' for nil:NilClass)
I created "delete_job.js.erb":
$('table').html("<%= j(render partial: 'finance_management/invoice/partials/new_subjects') %>");
But I got this error:
NoMethodError (undefined method `invoice_products=' for nil:NilClass):
The problem is that is not updating the div seems because getting nil error
Somebody can help me please?
According to this you must do this:
The controller invoice_controller.rb:
def show
@invoice= Invoice.find(params[:id])
@products = InvoiceProduct.find(:all,:conditions=>['invoice_id = ?',params[:id] ])
end
def destroy_job
@job = InvoiceProduct.find(params[:id])
@obj_invoice = Invoice.find(@job.invoice_id)
@job.destroy
end
The view "invoice/show.html.erb"
<%= @invoice.id %>
<div id="table"%>
<%= render :partial=>"invoice/partials/products" %>
</div>
Don't forget to create the partial view "invoice/partials/_products.erb" this will replace the div
<% @products.each do |i| %>
<%= i.name %>
<%= i.word %>
<%=link_to_remote(image_tag("image.png"),:url=>{:controller=>'invoice',:action => 'destroy_job',:id=>i.id})%>
<% end %>
Finally create "invoice/destroy_job.rjs"
page.replace_html 'table', :partial=>'invoice/partials/products'