簡體   English   中英

Rails 4,如何在create.js.erb中執行控制器特定的操作

[英]Rails 4, how to do a controller specific action in create.js.erb

我正在嘗試根據發生的頁面重新渲染部分內容。 我似乎無法使它正常工作,這總是我的事。

create.js.erb

$(".cart-text").html("<%= escape_javascript(render 'layouts/cart_text') %>")
<% if controller_name == "homes" && action_name == "pits" %>
    <% product_type = ProductType.where(name: 'Fosa') %>
    <% @products = Product.where(product_type: product_type) %>
<% elsif controller_name == "homes" && action_name == "coffin" %>
    <% product_type = ProductType.where(name: 'Féretro') %>
    <% @products = Product.where(product_type: product_type) %>
<% else %>
    <% actions = ProductType.where("name not in ('Fosa','Féretro')").uniq.pluck(:id) %>
    <% actions = -1 if actions.length == 0 %>
    <% product_type = ProductType.where('id IN (?)',actions) %>
    <% @products = Product.where(product_type: product_type) %>
<% end %>
<% @invoice_product = current_invoice.invoice_products.new %>
$(".list_products").html("<%= escape_javascript(render 'homes/list_products', products: @products, invoice_product: @invoice_product) %>")

我終於明白了:

它總是調用相同的控制器,即create。 我需要在所述控制器中創建一個實例變量,以通過該文件進行訪問。 這是我的最終代碼:

$(".cart-text").html("<%= escape_javascript(render 'layouts/cart_text') %>")
<% if @product_type_name == "Fosa" %>
    <% product_type = ProductType.where(name: 'Fosa') %>
    <% @products = Product.where(product_type: product_type) %>
<% elsif @product_type_name == "Féretro" %>
    <% product_type = ProductType.where(name: 'Féretro') %>
    <% @products = Product.where(product_type: product_type) %>
<% else %>
    <% actions = ProductType.where("name not in ('Fosa','Féretro')").uniq.pluck(:id) %>
    <% actions = -1 if actions.length == 0 %>
    <% product_type = ProductType.where('id IN (?)',actions) %>
    <% @products = Product.where(product_type: product_type) %>
<% end %>
<% @invoice_product = current_invoice.invoice_products.new %>
$(".list_products").html("<%= escape_javascript(render 'homes/list_products', products: @products, invoice_product: @invoice_product) %>")

您在erb條件中在此處擁有的大多數查詢都不屬於該視圖,並且沒有必要存在於視圖中。 為什么要有條件為每個不同的product_type_name“做某事”?

查看在每個條件中重復的內容。 沒有理由在每個條件中都重復對product_type的查詢,就像@products查詢一樣。 沒有理由在三個地方有相同的兩個查詢的重復,並且實際上沒有必要以條件作為開始。 很草率。 沒有充分的理由,product_type查詢基本上增加了權重和性能損失。 如果可以用product_type_id代替product_type_name,則可以繼續刪除所有的product_type查詢。 如果您實際上只具有對@product_type_name的訪問權限,那么沒關系,您仍然不需要product_type查詢。 考慮重復,考慮為什么要執行“操作”,然后考慮要執行的操作。

請注意,隨着product_type查詢被重復復制,唯一改變的是名稱。 您不需要為每個可能的不同product_type_name指定條件。 即使有理由查詢ProductType.where(name:@product_type_name),也沒有理由重復該查詢,而且肯定不會重復三遍。 考慮一下在每個條件中要執行的操作以及您要查詢的內容!

分解:

使用@product_type_name來查詢product_types表,因此可以獲取product_type。 對@products查詢重要的變量是product_type。 唯一重要的是您獲得了product_type,而@product_type_name僅用於獲得product_type。 就是這個 沒有理由在三個地方重復查詢。 實際上,一旦可以一起擺脫所有查詢,您甚至不需要查詢。

怎么會這樣?

考慮重復和條件,它們為什么用於它們? 您不需要多個查詢,您將條件查詢用於什么? 要獲取@products,您只需一個查詢。 如果“ product_type”列還不是外鍵,則應該是。 您可以使用@product_type_name,並且更改不大。 您所需要的只是一個帶有聯接的查詢,它是動態的。 而已。 沒有不必要的重復條件,也沒有條件內的重復查詢。 您可以消除所有條件,查詢可以執行您的條件,因為它是動態的。 無需基於名稱的條件,完全沒有必要。 該查詢應該能夠獲取@products,並且無論@product_type_name是什么都可以。 查詢將如下所示:

@products = Product.joins("INNER JOIN product_types ON product_types.id = products.product_type_id").where(product_types: {name: @product_type_name})

使上面的聯接查詢起作用,它消除了所有條件代碼和重復的查詢。 花一些時間考慮加入group和group_by。 在您的腦海中,將想法放在兩個條件中。 弄清楚為什么您認為那是這樣做的好方法,然后將其清除掉! 此外,除非您對性能不感興趣,否則請考慮僅刪除將用於@products的屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM