让用户回复活动?

Son*_*ack 1 ruby ruby-on-rails ruby-on-rails-4

所以我有一个应用程序,有点像meetup.com,用户可以创建事件,评论事件,搜索事件..但是,我想让用户点击"出席"按钮,然后点击活动/show.html.erb页面显示哪些用户正在参加...

目前用户has_many:events,以及事件belongs_to a:user.

因此,当用户点击他/她喜欢的事件时,他们会被定向到那个events/show.html.erb页面.我希望他们能够看到(X个人正在参加此活动),并且能够点击它并查看谁正在参加,也可以点击"参加活动".

那我该怎么做呢?

当前事件show.html.erb

<%= render 'shared/header' %>                                                                                                                                 

<div class="container">                                                                                                                                       
  <div class="row">                                                                                                                                           
    <div class="span3">                                                                                                                                       
      <%= render 'sidebar' %>                                                                                                                                 
    </div>                                                                                                                                                    
    <div class="span5">                                                                                                                                       
      <div class="new_event_form">                                                                                                                            
        <div class="line1"><h4>Create event</h4></div>                                                                                                        
        <%= form_for current_user.events.new, remote: true do |f| %>                                                                                          
          <h5>Event title:</h5>                                                                                                                               
          <div><%= f.text_field :title, placeholder: "Event title", required: true, autocomplete: :off %></div>                                               
          <h5>Event description:</h5>                                                                                                                         
          <div><%= f.text_area :description, placeholder: "Event description", required: true, autocomplete: :off %></div>                                    
          <h5>Event date:</h5>                                                                                                                                
          <div><%= f.text_field :date  %></div>                                                                                                               
          <h5>Event location:</h5>                                                                                                                            
          <div><%= f.text_field :location, placeholder: "Event location", required: true, autocomplete: :off %></div>                                         
          <div>                                                                                                                                               
            <%= f.submit "Create event", class: 'btn btn-primary' %>                                                                                          
            <%= link_to "Cancel", '#', class: 'btn cancel_event' %>                                                                                           
          </div><br />                                                                                                                                        
        <% end %>                                                                                                                                             
      </div>                                                                                                                                                  
      <div class="events_list">                                                                                                                               
        <!-- look in events/event.hmlt.erb -->                                                                                                                
        <h4><%= @event.title %> at <%= @event.location %></h4>                                                                                                
        <p><%= @event.description %></p>                                                                                                                      
        <h5><i class="fa fa-calendar-o"></i> <%= @event.date.strftime("%A, %B %d, %Y") %></h5>                                                                
        <h5><i class="fa fa-clock-o"></i> <%= @event.time %></h5>                                                                                             
        <h5><i class="fa fa-map-marker"></i> <%= @event.location %></h5>                                                                                      
      </div>                                                                                                                                                  
      <div class="name"></div>                                                                                                                                

            <%= form_for [@commentable, @comment], remote: true do |f| %> 
...........and so on...
Run Code Online (Sandbox Code Playgroud)

提前致谢!

Kre*_*eki 5

您的数据模型有问题.您的关联表示用户有很多事件,但事件只有一个用户.这是1:N关系,尝试改变它M:N,用这样的东西:

class User < ActiveRecord::Base
  has_and_belongs_to_many :events
end

class Event < ActiveRecord::Base
  has_any_belongs_to_many :users
end
Run Code Online (Sandbox Code Playgroud)

您需要为连接表提供必要的迁移,请在has_and_belongs_to_many此处阅读更多信息:

http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_and_belongs_to_many

然后,您将能够通过呼叫event.users和计数来让所有用户参加特定活动event.users.count.

如果您需要保留关于关系本身的更多信息,请考虑使用has_manywith :through选项.然后代码可能是这样的:

class User < ActiveRecord::Base
  has_many :attendances
  has_many :events, through: :attendances
end

class Attendance 
  belongs_to :user
  belongs_to :event
end

class Event < ActiveRecord::Base
  has_many :attendances
  has_many :users, through: :attendances
end

event.users.count # => `3` for example
Run Code Online (Sandbox Code Playgroud)