通过关联限制部分代码

发布于 2024-10-05 18:06:07 字数 1423 浏览 0 评论 0原文

我构建了一个 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 技术交流群。

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

发布评论

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

评论(1

想挽留 2024-10-12 18:06:07
# Membership model
named_scope :for_box, lambda {|box| {:conditions => {:box_id => box.id}}}

# User model
has_many :memberships

def member_of_box?(box)
  !memberships.for_box(box).blank?
end

# View
<% unless current_user.member_of_box?(box) %>
   # Show the form
<% end %>

免责声明:该代码未经测试。它可能需要进行一些小的改动。

# Membership model
named_scope :for_box, lambda {|box| {:conditions => {:box_id => box.id}}}

# User model
has_many :memberships

def member_of_box?(box)
  !memberships.for_box(box).blank?
end

# View
<% unless current_user.member_of_box?(box) %>
   # Show the form
<% end %>

Disclaimer: The code is not tested. It might need minor alterations.

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