如何为这种情况呈现 collection_check_box
How to render collection_check_box for this scenario
我是 mongodb 的新手,正在尝试与 Rails 一起学习。我正在使用 mongoid gem。
所以我的应用程序有一个用户资源,它应该有一个名字、电子邮件、地址和订阅。
姓名、电子邮件和地址字段可以采用用户希望键入的任何值。但是订阅是从可用订阅的主列表中选择的。我必须为可用的订阅呈现 collection_check_boxes,用户选择他想要的并提交表单。
我是这样设计的,
用户模型
名称:字符串
电子邮件:字符串
- 地址:字符串
- 订阅:数组
订阅模式
- 名称:字符串
除了订阅以用户形式提供可用订阅外,这两个模型之间没有任何关系。
一旦用户选择并提交表单,订阅 ID 将作为数组存储在用户模型中。
这在提交之前一切正常,但是当用户单击编辑以更改我不知道的订阅时,或者我无法呈现 collection_check_boxes 并勾选了他的原始订阅。
模型的创建方式是否存在设计缺陷?对于上面的场景,我无法使用 embeds_many、embedded_in 等关系。
对于以下代码,当用户尝试编辑时,我在编辑表单中收到此错误
undefined method `id' for "5733af2e54c870ee8190950b":String
<div class="checkbox">
<% if @user.new_record? %>
<%= f.collection_check_boxes(:subscriptions, Subscription.all, :id, :name) do |b| %>
<%= b.label class:"label-checkbox" do %>
<%= b.check_box + b.text %>
|
<%end%>
<% end %>
<% else %>
<%= f.collection_check_boxes(:subscriptions, Subscription.all, :id, :name, { checked: @user.subscriptions.map(&:id) }) do |b| %>
<%= b.label class:"label-checkbox" do %>
<%= b.check_box + b.text %>
|
<%end%>
<% end %>
<% end %>
</div>
我自己弄清楚了,所以集合复选框帮助器方法正在调用订阅 returned 上的 id
方法。但是我将订阅 ID 存储为用户模型中的字符串数组,无法对字符串值调用 id
方法。所以我在控制器中写了一个辅助方法 return 来自订阅模型的实际订阅对象。现在一切正常
def get_subscriptions(user)
Subscription.find(user.subscriptions)
end
helper_method :get_subscriptions
<% if @user.new_record? %>
<%= f.collection_check_boxes(:subscriptions, Subscription.all, :id, :name) do |b| %>
<%= b.label class:"label-checkbox" do %>
<%= b.check_box + b.text %>
|
<%end%>
<% end %>
<% else %>
<%= f.collection_check_boxes(:subscriptions, Subscription.all, :id, :name, { checked: get_subscriptions(@user).map(&:id) }) do |b| %>
<%= b.label class:"label-checkbox" do %>
<%= b.check_box + b.text %>
|
<%end%>
<% end %>
<% end %>
我是 mongodb 的新手,正在尝试与 Rails 一起学习。我正在使用 mongoid gem。 所以我的应用程序有一个用户资源,它应该有一个名字、电子邮件、地址和订阅。 姓名、电子邮件和地址字段可以采用用户希望键入的任何值。但是订阅是从可用订阅的主列表中选择的。我必须为可用的订阅呈现 collection_check_boxes,用户选择他想要的并提交表单。 我是这样设计的,
用户模型
名称:字符串
电子邮件:字符串
- 地址:字符串
- 订阅:数组
订阅模式
- 名称:字符串
除了订阅以用户形式提供可用订阅外,这两个模型之间没有任何关系。 一旦用户选择并提交表单,订阅 ID 将作为数组存储在用户模型中。 这在提交之前一切正常,但是当用户单击编辑以更改我不知道的订阅时,或者我无法呈现 collection_check_boxes 并勾选了他的原始订阅。 模型的创建方式是否存在设计缺陷?对于上面的场景,我无法使用 embeds_many、embedded_in 等关系。
对于以下代码,当用户尝试编辑时,我在编辑表单中收到此错误
undefined method `id' for "5733af2e54c870ee8190950b":String
<div class="checkbox">
<% if @user.new_record? %>
<%= f.collection_check_boxes(:subscriptions, Subscription.all, :id, :name) do |b| %>
<%= b.label class:"label-checkbox" do %>
<%= b.check_box + b.text %>
|
<%end%>
<% end %>
<% else %>
<%= f.collection_check_boxes(:subscriptions, Subscription.all, :id, :name, { checked: @user.subscriptions.map(&:id) }) do |b| %>
<%= b.label class:"label-checkbox" do %>
<%= b.check_box + b.text %>
|
<%end%>
<% end %>
<% end %>
</div>
我自己弄清楚了,所以集合复选框帮助器方法正在调用订阅 returned 上的 id
方法。但是我将订阅 ID 存储为用户模型中的字符串数组,无法对字符串值调用 id
方法。所以我在控制器中写了一个辅助方法 return 来自订阅模型的实际订阅对象。现在一切正常
def get_subscriptions(user)
Subscription.find(user.subscriptions)
end
helper_method :get_subscriptions
<% if @user.new_record? %>
<%= f.collection_check_boxes(:subscriptions, Subscription.all, :id, :name) do |b| %>
<%= b.label class:"label-checkbox" do %>
<%= b.check_box + b.text %>
|
<%end%>
<% end %>
<% else %>
<%= f.collection_check_boxes(:subscriptions, Subscription.all, :id, :name, { checked: get_subscriptions(@user).map(&:id) }) do |b| %>
<%= b.label class:"label-checkbox" do %>
<%= b.check_box + b.text %>
|
<%end%>
<% end %>
<% end %>