我有两个型号买单和卖单。在我的订单屏幕上,我有一个显示所有订单的部分,即买卖订单的组合。
我通过以下命令获取所有订单的清单:
def self.get_orders(p_uid)
b = Buyorder.where(“user_id=?”,p_uid).all.to_a
a = Sellorder.where(“user_id=?”,p_uid).all.to_a
return a+b
end
控制器执行:
@orders = Order.get_orders(session[:user_id])
在我看来,我会做以下事情:
<% for o in @orders %>
<tr><td> <%= link_to 'Delete', o, data: {confirm: 'Are you sure?'} , :method => :delete %></td> </tr>
<% end %>
我的routes.rb显示:
resources :orders
resources :buyorders
resources :sellorders
控制器买方控制器和卖方控制器都有
def destroy
….
end
但是我收到以下错误:
No route matches {:action=>"show", :controller=>"buyorders", :id=>nil} missing required keys: [:id]
destroy方法如下所示:
def destroy
Buyorder.where("id=? and user_id=?",params[:id],session[:user_id]).destroy_all
flash[:info] = 'Order has been deleted'
redirect_to main_index_path()
end
并且rake:路线显示:
buyorders GET /buyorders(.:format) buyorders#index
POST /buyorders(.:format) buyorders#create
new_buyorder GET /buyorders/new(.:format) buyorders#new
edit_buyorder GET /buyorders/:id/edit(.:format) buyorders#edit
buyorder GET /buyorders/:id(.:format) buyorders#show
PATCH /buyorders/:id(.:format) buyorders#update
PUT /buyorders/:id(.:format) buyorders#update
DELETE /buyorders/:id(.:format) buyorders#destroy
为什么?
PS:谁在降级所有答案以及问题?这是危险的。如果您认为这个问题很愚蠢,那么请提供一个有效的答案,我将非常乐意接受降级。解决方案:
self.get_orders的原始代码如下所示:
a = Buyorder.select('price,open_quantity').where("user_id=?",p_uid).all.to_a
b = Sellorder.joins('currency').select('id,currency1_id,currency2_id,ostatus,price,open_quantity').where("user_id=?",p_uid).all.to_a
一旦我使两行返回与
相同的结果a = Buyorder.joins('currency').select('id,currency1_id,currency2_id,ostatus,price,open_quantity').where("user_id=?",p_uid).all.to_a
b = Sellorder.joins('currency').select('id,currency1_id,currency2_id,ostatus,price,open_quantity').where("user_id=?",p_uid).all.to_a
错误消失了。
换句话说,如果组合两个数组,请确保它们返回的列完全相同。
答案 0 :(得分:0)
在视图中更新for
循环
<tr><td> <%= link_to 'Delete', o, :method => :delete, data: {confirm: 'Are you sure?'} %></td> </tr>
答案 1 :(得分:-1)
Url助手似乎无法使用您刚构建的阵列的元素构建路径。
试试这个:
if o.is_a?(Buyorder)
= link_to 'Delete', buyorder_path(id: o.id), data: {confirm: 'Are you sure?'} , :method => :delete
else
= link_to 'Delete', sellorder_path(id: o.id), data: {confirm: 'Are you sure?'} , :method => :delete
end