按 form_for 中的属性分组
我正在开发一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最终使用嵌套属性并验证首选项是否属于该用户类型(尽管我坚持认为应该有一种更多的“Rails 方式”来执行此操作。希望这可以帮助某人:
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: