具有嵌套属性的Best_In_Place内联编辑 [英] Best_In_Place inline edits with nested attributes
问题描述
我目前正在尝试使用best_in_place gem,以便在HTML表中进行内联编辑.我在购物车的展示视图中显示购物车.在购物车的显示视图中,我可以添加lineItems.创建LineItem时,还将使用lineItem_id创建新的Available记录,然后将其与lineitem一起显示在购物车中. Cart和LineItem表均来自外部数据库,因此,我无法向其中添加列,因此,我不能仅向LineItem添加可用的布尔属性.
I am currently trying to use the best_in_place gem in order to do inline editing within an HTML table. I am showing a cart in cart's show view. Within the cart's show view, I have the ability to add lineItems. When an LineItem is created, a new Available record is also created with a lineItem_id and then it's shown in the cart with its lineitem. Both the Cart and LineItem tables come from an external database and because of that, I can't add columns to so that is why I can't just add an available boolean attribute to the 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是否可用,那也很棒.我愿意接受任何想法!
I want to be able to edit the line_item.available.boolean within the html table which is on the cart show view using best_in_place but I am not having any luck.. Any help would be AMAZING! =] I know after reading around that it's not possible using nested attributes, but If I could get rid off the available model somehow and have a field in the show table that I can edit for a line_item to see whether or not the lineItem is available, that would also be great. I am open to any ideas!
推荐答案
首先,您的代码中有一些语法问题需要我们解决:
Firstly, there are a few syntax issues in your code that we need to fix:
@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.
Now, the answer:
As I explain in this Github issue, you need to pass a param
option and a url
option (used to be path
but that was deprecated in v3.0.0) to best_in_place.
默认url是best_in_place的第一个参数的更新操作.由于您的代码以best_in_place line_item.available
开头,因此默认为url_for(line_item.available.id)
.但是,您希望它修补LineItemsController的更新操作,即url_for(line_item)
The default url is the update action of the first argument to best_in_place. Since your code begins with best_in_place line_item.available
, this would default to url_for(line_item.available.id)
. However, you want it to PATCH the update action of the LineItemsController, i.e. url_for(line_item)
默认情况下,param选项假定您正在对AvailablesController进行PATCH,因此,这是Rails约定为了将available.boolean更新为值"1"所需的参数:
By default, the param option assumes you are PATCH'ing to the AvailablesController, so here are the parameters that Rails conventions require in order to update available.boolean to the value "1":
{"available"=>{"boolean"=>"1"}}
Available的ID已经在URL中,因此您唯一需要传递的额外参数是boolean
的新值.
The ID of the Available is in the URL already, so the only extra param you need to pass is the new value for boolean
.
但是,在您的情况下,您正在对LineItemsController进行修补,并且可用的模型接受嵌套的属性.这需要进行两项调整:
In your case, however, you are PATCH'ing to the LineItemsController and the available model accepts nested attributes. This requires two adjustments:
-
LineItem的ID已经在URL中,但Available ID则不在.我们在这里有两个选择:将Available的ID放入param选项中,或者通过在模型中将
update_only: true
传递给accepts_nested_attributes
来使ID不必要.update_only
方法可能不适用于您,具体取决于您的用例,但我发现它在大多数时候是最简单的方法,并免费提供了额外的安全层.
The ID of the LineItem is in the URL already, but the ID of the Available is not. We have two choices here: Either put the ID of the Available into the param option, or make the ID unnecessary by passing
update_only: true
toaccepts_nested_attributes
in the model. Theupdate_only
approach may not work for you depending on your use case, but I find that it is the simplest approach the vast majority of the time and adds an extra layer of security for free.
布尔选项需要正确嵌套,即:
The boolean option needs to be nested properly, i.e.:
line_items[available_attributes][boolean]
这样,当它到达服务器时,参数将是:
That way, when it gets to the server, the params will be:
{"line_item"=>{"available_attributes"=>{"id"=>"99","boolean"=>"1"}}}
# "99" is just an example of line_item.available.id
请注意,您将需要在控制器中允许这些属性,即:
Note that you will need to permit these attributes in the controller, i.e.:
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方法:
Putting it all together, here's your best_in_place method:
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
选项.
However, if at all possible, use the update_only
option.
# line_item.rb
accepts_nested_attributes_for :available, update_only: true
看看现在变得多么简单:
Look how much simpler it becomes now:
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]"
这篇关于具有嵌套属性的Best_In_Place内联编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!