[英]Best_In_Place inline edits with nested attributes
我目前正在嘗試使用best_in_place gem,以便在HTML表中進行內聯編輯。 我在購物車的展示視圖中顯示購物車。 在購物車的顯示視圖中,我可以添加lineItems。 創建LineItem時,還將使用lineItem_id創建新的Available記錄,然后將其與lineitem一起顯示在購物車中。 Cart表和LineItem表均來自外部數據庫,因此,我無法向其中添加列,因此,我不能僅向LineItem添加可用的布爾屬性。
**cart.rb
class Cart << AR::Base
has many LineItems
end
**line_item.rb
class LineItems <<AR::Base
belongs_to Cart
has_one :available
accepts_nested_attributes_for :available
end
**available.rb
class Available<<AR::Base
belongs_to LineItems
end
**views/cart/show.html.erb
@cart.lineitems.each do |line_items|
<td><%= line_item.price %></td>
<td><%=line_item.name %></td>
<td><%= best_in_place line_item.available.boolean, :boolean, :path => line_items_path, :type => type: :checkbox, collection: %w[No Yes] %></td>
end
我希望能夠使用best_in_place在購物車展示視圖上的html表中編輯line_item.available.boolean,但是我沒有任何運氣。任何幫助都將是驚人的! =]閱讀后,我知道無法使用嵌套屬性,但是如果我能以某種方式擺脫可用的模型,並在show表中有一個字段,可以編輯line_item以查看lineItem是否可用,那也很棒。 我願意接受任何想法!
首先,您的代碼中有一些語法問題需要我們解決:
@cart.lineitems.each do |line_item| # changed "line_items" to "line_item"
<td><%= line_item.price %></td>
<td><%=line_item.name %></td>
<td><%= best_in_place line_item.available, :boolean, :path => line_items_path, type: :checkbox, collection: %w[No Yes] %></td> # changed "line_item.available.boolean" to "line_item.available" and ":type => type: :checkbox" to "type: :checkbox"
end
正如我在這個Github問題中所解釋的那樣,您需要將param
選項和url
選項(以前是path
但在v3.0.0中已棄用)傳遞給best_in_place。
url
選項 默認URL是best_in_place的第一個參數的更新操作。 由於您的代碼以best_in_place line_item.available
,因此默認為url_for(line_item.available.id)
。 但是,您希望它修補LineItemsController的更新操作,即url_for(line_item)
param
選項 默認情況下,param選項假定您正在對AvailablesController進行PATCH,因此,這是Rails約定為了將available.boolean更新為值“ 1”所需的參數:
{"available"=>{"boolean"=>"1"}}
可用ID已在URL中,因此您需要傳遞的唯一額外參數是boolean
的新值。
但是,在您的情況下,您正在對LineItemsController進行修補,並且可用的模型接受嵌套的屬性。 這需要兩項調整:
LineItem的ID已經在URL中,但是Available ID則不在。 我們在這里有兩個選擇:將Available的ID放入param選項,或者通過將update_only: true
傳遞給模型中的accepts_nested_attributes
來使ID不必要。 根據您的用例, update_only
方法可能對您不起作用,但是我發現它在大多數情況下是最簡單的方法,並且免費添加了額外的安全層。
布爾選項需要正確嵌套,即:
line_items[available_attributes][boolean]
這樣,當到達服務器時,參數將是:
{"line_item"=>{"available_attributes"=>{"id"=>"99","boolean"=>"1"}}} # "99" is just an example of line_item.available.id
請注意,您將需要在控制器中允許這些屬性,即:
line_item.update(params.require(:line_item).permit( available_attributes: [:id, :boolean])) # You can remove `:id` if you are using the `update_only` option
放在一起,這是您的best_in_place方法:
best_in_place line_item.available, :boolean,
type: :checkbox,
url: line_item_path(line_item.id),
param: "line_item[available_attributes][id]=#{line_item.available.id}&line_item[available_attributes]"
但是,請盡可能使用update_only
選項。
# line_item.rb
accepts_nested_attributes_for :available, update_only: true
看看現在變得多么簡單:
best_in_place line_item.available, :boolean,
type: :checkbox,
url: line_item_path(line_item.id),
# Note: `url: line_item` might also work.
# If someone can confirm this in a comment, I can update this answer
param: "line_item[available_attributes]"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.