不知道我是否正朝着正确的方向前进,但缺乏网络开发知识使我无法完成以下任务:
In the application a user can setup settings which is an object. In the setting component we can subscribe or unsubscribe users from notifications To simplify the task for users and increase UX we decided to put all notifications on one form.
我认为应该可以Rails-3.2.8
。
所以我们有
class Setting has_many :notifications, :through => :email_notifications has_many :email_notifications
class Notification attr_accessible :name, :primary, :secondary has_many :settings, :through => :email_notifications has_many :email_notifications
class EmailNotification attr_accessible :notification_id, :setting_id, :primary, :secondary belongs_to :notification belongs_to :setting
在settings_controller
实例化所有notificatoins并将id传递给rich join association:
def new @setting = Setting.new @notifications = Notification.all @notifications.each do |n| @setting.email_notifications.build(:notification_id => n.id) end
然后我停留在视图中(所有解决方案都不可行,但无论如何我都列出了它们,所以你可以对解决方案及其出错的地方发表评论):
解决方案#1
<%= form_for @setting, :html => { :class => 'form-horizontal' } do |f| %>
<div class="control-group">
<%= f.label :email_periods, :class => 'control-label' %>
<div class="controls">
<%= f.select :email_periods, ["Daily", "Weekly", "Tuesdays & Thursdays"], :class => 'text_field' %>
</div>
</div>
<table class="table table-striped">
<thead>
<tr>
<th><%= 'Notification' %></th>
<th><%= 'Primary' %></th>
<th><%= 'Secondary' %></th>
<th><%= 'Reminder interval' %></th>
</tr>
</thead>
<tbody>
<% @setting.email_notifications.each do |s| %>
<tr>
<td><%= s.notification.name.to_s.humanize %></td>
<td><%= check_box 'setting[notification_ids]', :primary %></td>
<td><%= check_box 'setting[notification_ids]', :secondary %></td>
<td><%= 'possible ddl' %></td>
</tr>
<% end %>
</tbody>
</table>
<div class="form-actions">
<%= f.submit nil, :class => 'btn btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
settings_path, :class => 'btn' %>
</div>
<% end %>
但它给了我一个名字设置[notification_ids]的check_box实际上无法使用。
然后我想为email_notifications创建一个表单:
解决方案#2
<%= form_for @setting.email_notifications do |s| %>
<table class="table table-striped">
<thead>
<tr>
<th><%= 'Notification' %></th>
<th><%= 'Primary' %></th>
<th><%= 'Secondary' %></th>
<th><%= 'Reminder interval' %></th>
</tr>
</thead>
<tbody>
<% @setting.email_notifications.each do |s| %>
<tr>
<td><%= s.notification.name.to_s.humanize %></td>
<td><%= f.check_box s.primary %></td>
<td><%= f.check_box s.secondary %></td>
<td><%= 'possible ddl' %></td>
</tr>
<% end %>
</tbody>
</table>
<%= f.submit nil, :class => 'btn btn-primary' %></br>
<% end %>
然后它再次向我发出错误:
undefined method email_notification_..._email_notification_email_notification_email_notifications_path for #<#<Class:0x007f86942d16e0>:0x007f86979e02d0>
然后我决定使用form_for并在该表单中构建每个email_notification
:
<%= form_for @setting.email_notifications.build do |s| %>
<table class="table table-striped">
<thead>
<tr>
<th><%= 'Notification' %></th>
<th><%= 'Primary' %></th>
<th><%= 'Secondary' %></th>
<th><%= 'Reminder interval' %></th>
</tr>
</thead>
<tbody>
<% @setting.email_notifications.each do |s| %>
<tr>
<td><%= s.notification.name.to_s.humanize %></td>
<td><%= f.check_box s.primary %></td>
<td><%= f.check_box s.secondary %></td>
<td><%= 'possible ddl' %></td>
</tr>
<% end %>
</tbody>
</table>
<%= f.submit nil, :class => 'btn btn-primary' %></br>
<% end %>
但又是一个错误:
undefined method `email_notifications_path' for #:0x007f86979e02d0>
任何帮助将不胜感激。谢谢
P.S。由于某种原因,StackOverflow无法识别ruby样式继承<
,这就是我必须从示例中删除的原因。