在我的rails应用中,我有产品和订单模型。让我们说产品控制器看起来像这样:
<?php
// set the query
$query = 'posts_per_page=10';
// loop 1 - display most recent 10 posts
$queryObject = new WP_Query($query);
if ($queryObject->have_posts()) {
while ($queryObject->have_posts()) {
bm_ignorePost($queryPost->post->ID);
$queryObject->the_post();
the_title();
the_content();
}
}
// loop 2 - same query, get the next 10 posts
$queryObject = new WP_Query($query);
if ($queryObject->have_posts()) {
while ($queryObject->have_posts()) {
bm_ignorePost($queryPost->post->ID);
$queryObject->the_post();
the_title();
the_content();
}
}
?>
我已将索引视图中的所有产品列为div。当我点击它的div时,我想将产品的ID分配给订单(@ order.product_id = product.id)。
def index
@products = Product.all
@order = Order.new
end
任何想法如何做到这一点:?谢谢!
答案 0 :(得分:0)
您需要触发另一个包含产品ID的请求,并登陆将该ID分配给name
的新操作。在这种情况下,myfn (B _) = 200
行动中@order.product_id
毫无意义;相反,您可能需要产品内订单的嵌套资源,以及处理产品订单的新控制器。
@order = Order.new
products#index
现在,resources :products do
resources :orders
end
视图可以链接到产品中新订单的嵌套路线。这将生成类似def index
@products = Product.all
end
:
products#index
最后,您可以使用路线中指定的产品呈现新的订单表单:
/products/123/orders/new