使用round_robin_tournament gem时获取未定义的局部变量或方法`tournament'

时间:2018-03-25 03:49:44

标签: ruby-on-rails tournament

我正在尝试使用gem round_robin_tournament,如下所述:

Github

但是我收到了错误:

  

#<#的未定义局部变量或方法`锦标赛'   :0x00007ffb591d54a0>

我按照该链接的自述文件部分中的说明进行操作。我还是Ruby on rails的新手,所以它可能很简单:)。

这是我的代码:

Message_Controller.rb:

class MessagesController < ApplicationController
  before_filter :authenticate_user!

  def create
    @conversation = Conversation.find(params[:conversation_id])
    @message = @conversation.messages.build(message_params)
    @message.user_id = current_user.id
    @message.save!

    @path = conversation_path(@conversation)
  end

  private

  def message_params
    params.require(:message).permit(:body)
  end

  def tournament
    require "round_robin_tournament"

    # Compute all the possible teams for each day in the classroom
    students = %w(John Paul Ringo George)
    teams = RoundRobinTournament.schedule(students)

    # Print for each day, each team
    teams.each_with_index do |day, index|
      day_teams = day.map { |team| "(#{team.first}, #{team.last})" }.join(", ")
      puts "Day #{index + 1}: #{day_teams}"
    end

    # Day 1: (Paul, George), (Ringo, John)
    # Day 2: (Ringo, George), (John, Paul)
    # Day 3: (John, George), (Paul, Ringo)
  end
end

routes.rb中:

Rails.application.routes.draw do

  resources :posts
  resources :comments, only: [:create, :destroy]
  devise_for :users
  resources :users do
    member do
      get :friends
      get :followers
      get :deactivate
      get :mentionable
    end
  end

  resources :events do
    collection do
      get :calendar
    end
  end

  authenticated :user do
    root to: 'home#index', as: 'home'
  end
  unauthenticated :user do
    root 'home#front'
  end

  resources :conversations do
    resources :messages
  end

  match :follow, to: 'follows#create', as: :follow, via: :post
  match :unfollow, to: 'follows#destroy', as: :unfollow, via: :post
  match :like, to: 'likes#create', as: :like, via: :post
  match :unlike, to: 'likes#destroy', as: :unlike, via: :post
  match :find_friends, to: 'home#find_friends', as: :find_friends, via: :get
  match :about, to: 'home#about', as: :about, via: :get

  # The priority is based upon order of creation: first created -> highest priority.
  # See how all your routes lay out with "rake routes".

  # You can have the root of your site routed with "root"
  # root 'welcome#index'

  # Example of regular route:
  #   get 'products/:id' => 'catalog#view'

  # Example of named route that can be invoked with purchase_url(id: product.id)
  #   get 'products/:id/purchase' => 'catalog#purchase', as: :purchase

  # Example resource route (maps HTTP verbs to controller actions automatically):
  #   resources :products

  # Example resource route with options:
  #   resources :products do
  #     member do
  #       get 'short'
  #       post 'toggle'
  #     end
  #
  #     collection do
  #       get 'sold'
  #     end
  #   end

  # Example resource route with sub-resources:
  #   resources :products do
  #     resources :comments, :sales
  #     resource :seller
  #   end

  # Example resource route with more complex sub-resources:
  #   resources :products do
  #     resources :comments
  #     resources :sales do
  #       get 'recent', on: :collection
  #     end
  #   end

  # Example resource route with concerns:
  #   concern :toggleable do
  #     post 'toggle'
  #   end
  #   resources :posts, concerns: :toggleable
  #   resources :photos, concerns: :toggleable

  # Example resource route within a namespace:
  #   namespace :admin do
  #     # Directs /admin/products/* to Admin::ProductsController
  #     # (app/controllers/admin/products_controller.rb)
  #     resources :products
  #   end
end

index.html.erb:

<div class="row">
  <div class="col-lg-3">
    <%= render 'shared/user_info' %>
  </div>
  <div id="newsfeed" class="col-lg-6">
    <%= render 'posts/form' %>
    <br>
    <div id="activities">
      <% if @activities.empty? %>
        <div class="well">
          <%= render('shared/no_resource', resources: 'Activities') %>
        </div>
      <% else %>
        <%= render_activities(@activities, layout: '/shared/activity') %>
      <% end %>
    </div>
    <%= render 'shared/paginate', resources: @activities %>
  </div>
  <div class="col-lg-3">
    <%= render 'shared/links' %>
      <% @users.each_with_index do |user, index| %>
    <tr>
      <td><%= index +=1 %></td>
      <td><%= user.name %></td>
      <td>
        <%= link_to "Send Message", "#", class: "btn btn-success btn-xs start-conversation",
                    "data-sid" => current_user.id, "data-rip" => user.id %>
      </td>
    </tr>
<% end %>
  </div>

  <div>
    <%= tournament %>
  </div>
</div>

非常感谢您提供帮助。

新守则:

控制器:

class MessagesController < ApplicationController
  helper_method :tournament
  before_filter :authenticate_user!

  def create
    @conversation = Conversation.find(params[:conversation_id])
    @message = @conversation.messages.build(message_params)
    @message.user_id = current_user.id
    @message.save!

    @path = conversation_path(@conversation)
  end
  # controller
  def tournament
      require "round_robin_tournament"

      RoundRobinTournament.schedule %w(John Paul Ringo George)
  end

  private

  def message_params
    params.require(:message).permit(:body)
  end
end

查看:

<div class="row">
  <div class="col-lg-3">
    <%= render 'shared/user_info' %>
  </div>
  <div id="newsfeed" class="col-lg-6">
    <%= render 'posts/form' %>
    <br>
    <div id="activities">
      <% if @activities.empty? %>
        <div class="well">
          <%= render('shared/no_resource', resources: 'Activities') %>
        </div>
      <% else %>
        <%= render_activities(@activities, layout: '/shared/activity') %>
      <% end %>
    </div>
    <%= render 'shared/paginate', resources: @activities %>
  </div>
  <div class="col-lg-3">
    <%= render 'shared/links' %>
      <% @users.each_with_index do |user, index| %>
    <tr>
      <td><%= index +=1 %></td>
      <td><%= user.name %></td>
      <td>
        <%= link_to "Send Message", "#", class: "btn btn-success btn-xs start-conversation",
                    "data-sid" => current_user.id, "data-rip" => user.id %>
      </td>
    </tr>
<% end %>
  </div>
<div>
<% tournament.each_with_index do |day, index| %>
  <% day_teams = day.map { |team| "(#{team.first}, #{team.last})" }.join(", ") %>

  <%= "Day #{index + 1}: #{day_teams}" %>
<% end %>
</div>
</div>

1 个答案:

答案 0 :(得分:0)

这看起来根本不像宝石那么多,只是默认情况下,视图中无法访问控制器中定义的方法。您可以使用helper_method告诉rails使视图可用的方法:

class MessagesController < ApplicationController
  helper_method :tournament

  # rest of your controller
end

我不确定这种方法是否适合private(如果它不起作用,请尝试改为方法public)。另请注意,puts仍然会在rails中输出到$stdout,因此如果您想要视图中的输出,您可能只想从方法返回teams然后执行视图中的那个迭代(未经测试,但可能会起作用):

# controller
def tournament
    require "round_robin_tournament"

    RoundRobinTournament.schedule %w(John Paul Ringo George)
end

# view
<% tournament.each_with_index do |day, index| %>
  <% day_teams = day.map { |team| "(#{team.first}, #{team.last})" }.join(", ") %>

  <%= "Day #{index + 1}: #{day_teams}" %>
<% end %>