按 form_for 中的属性分组

发布于 2024-11-27 20:42:56 字数 1628 浏览 2 评论 0原文

我正在开发一个 Rails 项目,该项目需要根据学校的不同用户类型的偏好。因此我有这些模型:

class Preference < ActiveRecord::Base

  belongs_to :school
  belongs_to :privilege
  belongs_to :user_type

end

class Privilege < ActiveRecord::Base

  has_many :preferences

end

所以在我看来,我希望编辑给定学校的首选项,并且希望按用户类型进行分离和排序。因此,例如我完成的视图将如下所示:

    <form accept-charset="UTF-8" action="/preferences/edit" method="post">
        <table>
            <thead>
                    <td>Pivilege</td>
                    <td>Allowed</td>
                </thead>
                <tbody>
                <h2>Student User type</h2>
                <tr>
                    <td>Privilege 1<td>
                    <td>checkbox for privilege 1<td>
                </tr>
<tr>
                    <td>Privilege 2<td>
                    <td>checkbox for privilege 2<td>
                </tr>
                <h2>Employee User type</h2>
                <tr>
                    <td>Privilege 1<td>
                    <td>checkbox for privilege 1<td>
                </tr>
<tr>
                    <td>Privilege 2<td>
                    <td>checkbox for privilege 2<td>
                </tr>               
            </tbody>    
        </table>
    </form>

问题是我不知道如何在 (form_for @preferences do |f|) 块内迭代并按 user_types 对该数组 (@preferences) 进行分组。

有人可以帮助我吗?提前致谢

I am working on a Rails project that requires preferences for diferent user types depending on which shool. Thus i have these models:

class Preference < ActiveRecord::Base

  belongs_to :school
  belongs_to :privilege
  belongs_to :user_type

end

class Privilege < ActiveRecord::Base

  has_many :preferences

end

So in my view, i wish to edit the preferences for a given school and i wish to separate and order by user type. So for example my finished view would look like this:

    <form accept-charset="UTF-8" action="/preferences/edit" method="post">
        <table>
            <thead>
                    <td>Pivilege</td>
                    <td>Allowed</td>
                </thead>
                <tbody>
                <h2>Student User type</h2>
                <tr>
                    <td>Privilege 1<td>
                    <td>checkbox for privilege 1<td>
                </tr>
<tr>
                    <td>Privilege 2<td>
                    <td>checkbox for privilege 2<td>
                </tr>
                <h2>Employee User type</h2>
                <tr>
                    <td>Privilege 1<td>
                    <td>checkbox for privilege 1<td>
                </tr>
<tr>
                    <td>Privilege 2<td>
                    <td>checkbox for privilege 2<td>
                </tr>               
            </tbody>    
        </table>
    </form>

The problem is i do not know how to itarate within a (form_for @preferences do |f|) block and group this array (@preferences) by user_types.

Can anybody help me? Thanxs in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离不开的别离 2024-12-04 20:42:56

我最终使用嵌套属性并验证首选项是否属于该用户类型(尽管我坚持认为应该有一种更多的“Rails 方式”来执行此操作。希望这可以帮助某人:

<%= form_for @school, :url => {:controller => :preferences, :action => :update, :id => @school.id} do |f| %>
        <h2>Change Preferences</h2>     
    <% UserType.all.each do |ut|%>
         <h3><%= ut.name%></h3>
            <table>

                <thead>
                    <td>Privilege</td>
                    <td>Allowed</td>
                </thead>

                <tbody>

                <%= f.fields_for :preferences do |pref| %>
                  <% if pref.object.user_type == ut%>
                    <tr>
                      <td><%= pref.object.privilege.name%></td>
                      <td><%= pref.check_box :allowed, {:allowed => pref.object.allowed} %></td>
                    </tr>
                  <% end %>
                <% end%>
                </tbody>    

          </table>      
    <% end %>

I ended up using nested attributes and verifying if the preference belongs to that user type (although I insist that there should be a more "Rails way" of doing it. Hopes this helps somebody:

<%= form_for @school, :url => {:controller => :preferences, :action => :update, :id => @school.id} do |f| %>
        <h2>Change Preferences</h2>     
    <% UserType.all.each do |ut|%>
         <h3><%= ut.name%></h3>
            <table>

                <thead>
                    <td>Privilege</td>
                    <td>Allowed</td>
                </thead>

                <tbody>

                <%= f.fields_for :preferences do |pref| %>
                  <% if pref.object.user_type == ut%>
                    <tr>
                      <td><%= pref.object.privilege.name%></td>
                      <td><%= pref.check_box :allowed, {:allowed => pref.object.allowed} %></td>
                    </tr>
                  <% end %>
                <% end%>
                </tbody>    

          </table>      
    <% end %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文