将数组放入渲染html导致语法错误

时间:2018-04-03 06:12:11

标签: ruby-on-rails arrays controller

我正在我的ruby on rails项目中创建一个锦标赛。我设法在html端打印出一个。比我设法在控制台屏幕上组织打印多个锦标赛。所以我想我会将所有元素移动到一个数组,而不是使用

将该数组打印到视图中
@teamArray = render html: '<div>'puts a'</div>'.html_safe

但是,我收到了一些错误。

syntax error, unexpected tIDENTIFIER, expecting keyword_end
...rray = render html: '<div>'puts a end'</div>'.html_safe
...                           ^~~~

这是我的代码:

控制器

class TourniesController < ApplicationController
  before_action :set_tourny, only: [:show, :edit, :update, :destroy, :tournament]
  helper_method :tournament
  # GET /tournies
  def index
    @tournies = Tourny.all
  end

  # GET /tournies/1
  def show
  end

  # GET /tournies/new
  def new
    @tourny = Tourny.new
  end

  # GET /tournies/1/edit
  def edit
  end

  # POST /tournies
  def create
    @tourny = Tourny.new(tourny_params)

    if @tourny.save
      redirect_to @tourny, notice: 'Tourny was successfully created.'
    else
      render :new
    end
  end

  # PATCH/PUT /tournies/1
  def update
    if @tourny.update(tourny_params)
      redirect_to @tourny, notice: 'Tourny was successfully updated.'
    else
      render :edit
    end
  end

  # DELETE /tournies/1
  def destroy
    @tourny.destroy
    redirect_to tournies_url, notice: 'Tourny was successfully destroyed.'
  end

  def tournament
    a = []
    for team in 0..Tourny.all.count do 
      require "round_robin_tournament"
      teams = RoundRobinTournament.schedule ((Tourny.where(id: [team]).pluck(:teamName)).join("").split(" "))

      teams.each_with_index do |day, index|
        day_teams = day.map { |team| "(#{team.first}, #{team.last})" }.join(", ")
        puts "Day #{index + 1}: #{day_teams}"
        a.push("Day #{index + 1}: #{day_teams}")
      #render html: '<div>"Day #{index + 1}: #{day_teams}"</div>'.html_safe
      end

    end
    @teamArray = render html: '<div>'puts a'</div>'.html_safe
    #Tourny.pluck(:teamName)
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_tourny
      @tourny = Tourny.find(params[:id])
    end

    # Only allow a trusted parameter "white list" through.
    def tourny_params
      #params.fetch(:tourny, {})
      params.require(:tourny).permit(:noteams, :teamName, :tournamentCode)
    end
end

index.html.erb

<p id="notice"><%= notice %></p>

<h1>Tournies</h1>

<table>
  <thead>
    <tr>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @tournies.each do |tourny| %>
      <tr>
        <td><%= link_to 'Show', tourny %></td>
        <td><%= link_to 'Edit', edit_tourny_path(tourny) %></td>
        <td><%= link_to 'Destroy', tourny, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

<%= link_to 'New Tourny', new_tourny_path %>

<%= tournament %>

路线

Rails.application.routes.draw do

  resources :tournies
  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

  get '/tournament/bracket' => 'tournament#tournament'

  # 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

如果您知道错误是什么,请告诉我。我花了好几天试图解决这个问题。

1 个答案:

答案 0 :(得分:0)

修改以下代码

@teamArray = render html: '<div>'puts a'</div>'.html_safe

as

@teamArray = render html: "<div>#{a}</div>".html_safe

您可以阅读有关呈现HTML here

的更多信息