Rails,从html获取信息

时间:2016-06-27 11:25:46

标签: ruby-on-rails forms

在我的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

任何想法如何做到这一点:?谢谢!

1 个答案:

答案 0 :(得分:0)

您需要触发另一个包含产品ID的请求,并登陆将该ID分配给name的新操作。在这种情况下,myfn (B _) = 200 行动中@order.product_id毫无意义;相反,您可能需要产品内订单的嵌套资源,以及处理产品订单的新控制器。

配置/ routes.rb中

@order = Order.new

应用程序/控制器/ products_controller

products#index

现在,resources :products do resources :orders end 视图可以链接到产品中新订单的嵌套路线。这将生成类似def index @products = Product.all end

的网址

应用程序/视图/产品/ index.html.shaml

products#index

最后,您可以使用路线中指定的产品呈现新的订单表单:

应用程序/控制器/ orders_controller

/products/123/orders/new