1

不知道我是否朝着正确的方向前进,但缺乏网络开发知识使我无法完成以下任务:

在应用程序中,用户可以设置作为对象的设置。在设置组件中
我们可以订阅或取消订阅用户的通知来简化用户的任务
并增加用户体验,我们决定将所有通知放在一个表单上。

我相信这应该是可能的Rails-3.2.8

所以我们有

班级设置

  has_many :notifications, :through => :email_notifications
  has_many :email_notifications
类通知
  attr_accessible :name, :primary, :secondary

  has_many :settings, :through => :email_notifications
  has_many :email_notifications
类电子邮件通知
  attr_accessible :notification_id, :setting_id, :primary, :secondary
  属于_to:通知
  属于_to:设置

settings_controller实例化所有通知并将 id 传递给丰富的加入关联:

定义新
  @setting = Setting.new
  @notifications = Notification.all
  @notifications.each 做 |n|
    @setting.email_notifications.build(:notification_id => n.id)
  结尾

然后我停留在视图中(所有解决方案都不可行,但我还是列出了它们,因此您可以评论解决方案及其出错的地方):

解决方案#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 %>

但它给了我一个名为 setting[notification_ids] 的复选框,实际上它是不可用的。

然后我想为 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 %>

但是又报错了:

#:0x007f86979e02d0> 的未定义方法 `email_notifications_path'

任何帮助,将不胜感激。谢谢

PS 由于某种原因,StackOverflow 无法识别 ruby​​ 样式继承<,这就是我必须从示例中删除的原因。

4

0 回答 0