通过关联限制部分代码
我构建了一个 Ruby on Rails 应用程序,可以让用户跟踪他们的锻炼情况。用户有_很多次锻炼。此外,如果用户是健身房所有者,则可以创建一个盒子(健身房)。目的是过滤用户的活动,以便他们只能看到与其健身房相关的信息。然后,用户可以通过成员资格模型指定他们是否是该框的成员。 Membership 表分别在membership.box_id 和user.id 列中收集@box.id 和current_user.id。
用户通过 /views/boxes/show.html.erb 视图中的以下表单进行关联:
<% remote_form_for Membership.new do |f| %>
<%= f.hidden_field :box_id, :value => @box.id %>
<%= f.hidden_field :user_id, :value => current_user.id %>
<%= submit_tag "I am a member of this box" , :class => '' %>
<% end %>
然后,我在框中显示页面中显示属于该框成员的所有用户。
<% @box.users.each do |user| %>
<%= link_to (user.username), user %><br/>
<% end %>
我试图将表单限制为仅允许还不是该框成员的用户,但我不确定如何编写 <% except ... %>
语句。
以下是其余的协会:
用户
class User < ActiveRecord::Base
has_many :boxes
has_many :workouts, :dependent => :destroy
end
锻炼
class Workout < ActiveRecord::Base
belongs_to :user
belongs_to :box
end
盒
class Box < ActiveRecord::Base
belongs_to :user
has_many :users, :through => :memberships
has_many :workouts, :through => :users
has_many :memberships
end
会员资格
class Membership < ActiveRecord::Base
belongs_to :user
belongs_to :box
end
I have built a ruby on rails app that lets users track their workouts. User has_many workouts. In addition, a User can create a box (gym) if they are a gym owner. The purpose is to filter activity of users such that they can only see information related to their gym. Users can then specify if they are a member of that box through a Membership model. The Membership table collects @box.id and current_user.id in the membership.box_id and user.id columns respectively.
The user associates through the following form in the /views/boxes/show.html.erb view:
<% remote_form_for Membership.new do |f| %>
<%= f.hidden_field :box_id, :value => @box.id %>
<%= f.hidden_field :user_id, :value => current_user.id %>
<%= submit_tag "I am a member of this box" , :class => '' %>
<% end %>
I then display, in the box show page all the users who are members of that box.
<% @box.users.each do |user| %>
<%= link_to (user.username), user %><br/>
<% end %>
I am trying to restrict the form to only users who are not already members of that box but I am not sure how to write the <% unless ... %>
statement.
Here are the rest of the associations:
User
class User < ActiveRecord::Base
has_many :boxes
has_many :workouts, :dependent => :destroy
end
Workout
class Workout < ActiveRecord::Base
belongs_to :user
belongs_to :box
end
Box
class Box < ActiveRecord::Base
belongs_to :user
has_many :users, :through => :memberships
has_many :workouts, :through => :users
has_many :memberships
end
Membership
class Membership < ActiveRecord::Base
belongs_to :user
belongs_to :box
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
免责声明:该代码未经测试。它可能需要进行一些小的改动。
Disclaimer: The code is not tested. It might need minor alterations.