Rails日历显示重复信息

时间:2015-08-02 11:05:23

标签: ruby-on-rails calendar

我有一个日历,我想显示"访问"属于"用户"和"分支"。这是有效的,因为每次访问都会在正确的日期显示正确的信息,并且只显示给正确的用户。但由于某种原因,每个日历条目也显示其数据库条目的所有内容的数组,如下所示:

[#<Visit id: 2, location: nil, date_from: "2015-08-12", time_from: "2000-01-01 10:30:00", date_to: "2015-08-20", time_to: "2000-01-01 09:15:00", comment: "trfghytrfg", branch_id: 2, user_id: 2>] 

显然这看起来并不好看,我不希望它出现,但我无法看到我的代码中的来源。如果有人能够看到我出错的地方会很棒。

visits_controller.rb:

class VisitsController < ApplicationController
  before_filter :authenticate_user!, except: [ :new]
  before_action :set_visit, only: [:show, :edit, :update, :destroy]

  def index
    @visits = Visit.where(user_id: current_user.id)
    @visits_by_date = @visits.group_by(&:date_from)
    @date = params[:date] ? Date.parse(params[:date]) : Date.today
    @users = User.all

  end

  def show
  end

  def new
    @visit = Visit.new
    @branch = Branch.all
  end

  def edit
  end

  def create
    @visit = current_user.visits.new(visit_params)
    respond_to do |format|
      if @visit.save
        format.html { redirect_to @visit, notice: 'Visit was successfully created.' }
        format.json { render :show, status: :created, location: @visit }
      else
        format.html { render :new }
        format.json { render json: @visit.errors, status: :unprocessable_entity }
      end
    end
  end

def update
    respond_to do |format|
      if @visit.update(visit_params)
        format.html { redirect_to @visit, notice: 'Visit was successfully updated.' }
        format.json { render :show, status: :ok, location: @visit }
      else
        format.html { render :edit }
        format.json { render json: @visit.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @visit.destroy
    respond_to do |format|
      format.html { redirect_to visits_url, notice: 'Visit was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def visit_params
      params.require(:visit).permit(:location, :date_from, :time_from, :date_to, :time_to, :comment, :branch_id, :user_id)
    end
end

visit.rb:

class Visit < ActiveRecord::Base
    belongs_to :branch  
    belongs_to :user #:as => 'created_by' 
    validates_uniqueness_of :time_from, :scope => [:date_from, :location], :message=>"slot is already taken on selected date"

end

calendar_helper.rb:

module CalendarHelper
 def calendar(date = Date.today, &block)
    Calendar.new(self, date, block).table
  end


class Calendar < Struct.new(:view, :date, :callback)
    HEADER = %w[Sunday Monday Tuesday Wednesday Thursday Friday Saturday]
    START_DAY = :sunday

    delegate :content_tag, to: :view

    def table
      content_tag :table, class: "calendar table table-bordered table-striped" do
        header + week_rows
      end
    end

    def header
      content_tag :tr do
        HEADER.map { |day| content_tag :th, day }.join.html_safe
      end
    end

    def week_rows
      weeks.map do |week|
        content_tag :tr do
          week.map { |day| day_cell(day) }.join.html_safe
        end
      end.join.html_safe
    end

    def day_cell(day)
      content_tag :td, view.capture(day, &callback), class: day_classes(day)
    end

    def day_classes(day)
      classes = []
      classes << "today" if day == Date.today
      classes << "not-month" if day.month != date.month
      classes.empty? ? nil : classes.join(" ")
    end

    def weeks
      first = date.beginning_of_month.beginning_of_week(START_DAY)
      last = date.end_of_month.end_of_week(START_DAY)
      (first..last).to_a.in_groups_of(7)
    end
end
end

index.html.erb:

<div id="visits">
<h2 id="month">

    <%= link_to "<-", date: @date.prev_month %> 
    <%= @date.strftime("%B %Y") %>
    <%= link_to "->", date: @date.next_month %>
</h2>
    <%= calendar @date do |date| %>
    <%= date.day %>
    <% if @visits_by_date[date] %>
    <ul>
        <%= @visits_by_date[date].each do |visit| %>
        <%= visit.branch.branch_name %>
    <% end %>
    </ul>
    <% end %>
<% end %>
</div>

1 个答案:

答案 0 :(得分:1)

<%= @visits_by_date[date].each do |visit| %>

应该是:

<% @visits_by_date[date].each do |visit| %>