Reputation: 433
I need to have a button in my view, on click it should delete items from my DB table and refresh the view with updated DB contents. Below is the code in my index.html.erb file
<div id="table-record">
<div class="table-order-detail" id="position1">
<div class="table-inner-border">
<% if @orders%>
<div class="bottom-border1"><%= "Table"[email protected] %>
</div>
<div class="bottom-border1">
<div class="item-name">Item</div> <div class="qty">Qty</div>
</div>
<% @orders.each do |order| %>
<% if (order.itmstatus == "displayed") %>
<div class="item-name"><%= order.itemname %></div>
<div class="qty"><%= order.quantity %></div>
<% else %>
<div class="new-item-name"><%= order.itemname %></div>
<div class="qty"><%= order.quantity %></div>
<% end %>
<% end %>
<% end %>
<% button_to "Done", {:action => "doneorder", :id => @orders.first.tableno, :controller => "kitchens"}, :method => :delete %>
</div></div>
@orders.first.tableno is the key based on which I need to delete rows from my DB table(not sure if I need to pass this in id field, but I found this syntax in one of the related posts)
Below is the code in my controller (kitchens). The controller had initially only index method defined. Now I have added another method doneorder which I need to call on my button click.
def doneorder
List.where(:tableno => params[:id])).delete_all
redirect_to :action => :index
end
In above method I need to delete rows corresponding to id passed from button click and then redirect to index action. Index action will fetch updated rows from lists table and display updated contents.
I am totally beginner in rails and I have tried numerous things from related posts including link_to but nothing seems to be working. With above code in place I get below error
No route matches {:action=>"doneoreder", :id=>"01", :controller=>"kitchens"}
In my routes file I have given
resources :kitchens do
put :done_order, on: :member
end
Please help. Thanks.
Upvotes: 1
Views: 223
Reputation: 1892
your route has done_order
but your action is doneorder
so either change your route or the action name.
edit: you also don't need to enclose the button_to in a button tag.
you also need to add :method => :put
in your button_to call. i hope you did not follow the route change in rajarshi's answer. if you did, change it back to put
instead of delete
Upvotes: 0
Reputation: 12320
in routes
resources :kitchens do
delete :doneorder
end
in view
<%= button_to "Done", { :controller => "kitchens", :action => "doneorder", :id => @orders.first.tableno}, :method => :delete %>
<button type="submit" id="1">
please remove this html code no more required
Upvotes: 1