站点不断为更新的实例分配新索引

时间:2014-05-09 23:15:44

标签: ruby-on-rails ruby indexing

所以我的网站现在有一个地址列表,其下拉列表为LocationsLocations在创建时会被编入索引,但是当位置更新时它会更改索引。所以基本上问题是我想要改变Location 1。说我也有6个地点。当我按Location 1上的更新时,Location实例移动到索引的末尾(#6)。我知道问题出在哪里,但我不确定如何修复它。

我的展示页

.dropdown-panel
    .dropdown-info
      - if events.any?
        %a.button.dropdown{"data-dropdown" => "drop1"} Locations
      - else
        %a.button No Locations
      %ul#drop1.f-dropdown{"data-dropdown-content" => "", "data-tab" => ''}
        - events.each_with_index do |event, index|
          %li
            %a#dropDownNav{:href => "#panel1-#{index + 1}"} Location #{index + 1}
    .dropdown-add
      - if current_user
        %a.button#add_event{:href => "#new_event"} +
  .tabs-content
    - events.each_with_index do |event, index| <---I feel this is the issue(See Below)
      -if event.user_id == current_user.id
        .content{id: "panel1-#{index + 1}", class: index.zero? ? 'active' : ''}  
          = render 'form', event: event, index: index
    - if current_user
      .content{id: "new_event"}
        = render 'form'

我认为以上内容存在.tabs内容下的问题。每次重新加载该列表时,each_with_index都会分配一个新索引。我无法弄清楚如何将其更改为仅为新事件指定索引。

我不确定_form.html.haml部分是否会有所帮助,但这里是......

.form-content
      - if current_user
        / - event.errors.each do |attr, msg|
        /   = msg
        = form_for event do |f|
          %fieldset.location-entry
            - title = f.object.new_record? ? "New Location" : "Location #{index + 1}"
            = f.hidden_field :date, value: date
            .alert-box.alert{"data-alert" => ""}
              An address is required
              %a.close{:href => "#"} &times;
            %h4= title
            - flash.each do |name, msg|
              = content_tag :div, msg, class: name
            .fields
              %dl
                %dt
                  = f.label :address, "Enter Location", :required => true
                %dd
                  = f.text_field :address, placeholder: 'Enter Address', class: 'address-input'
              %dl
                %dt
                  = f.label :start, "Enter Timeframe"
                %dd.time-entry
                  #timestart= f.time_select :start, { combined: true, minute_step: 15, ampm: true, :time_separator => ""}
                  .to
                    %p to
                  #time_end= f.time_select :end_time, { combined: true, minute_step: 15, ampm: true, :time_separator => ""}
            %dl.entry-submit
              - if !event.address
                = f.submit 'Enter Location', class: 'button', id: 'newevent'
              - if event.address
                = f.submit 'Update Location', class: 'button', id: 'updateevent', method: :put
                .remove-location
                  = link_to 'Remove Location', event, method: :delete,  data: { confirm: 'Are you sure?' }

另外请告诉我这是一个很好的解释它或我需要添加什么来使其更好。

我的事件控制器

class EventsController < ApplicationController
  before_action :verify_user_or_admin

  def update
    params[:event].parse_time_select! :start
      params[:event].parse_time_select! :end_time
    event = current_user.events.find(params[:id])
    respond_to do |format|
      if event.update(event_params)
        format.html { redirect_to event_path(event.start.to_date), notice: 'Event was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { redirect_to event_path(event.date), alert: 'There was an error creating your event.' }
        format.json { render json: event.errors, status: :unprocessable_entity }
      end
    end
  end

  def show
    if current_user
      @locations= current_user.events.where( "date(start) = ?", date).all
    elsif current_admin
      @locations = Event.where("date(start) = ?", date).all
    end
    @hash = Gmaps4rails.build_markers(@locations) do |location, marker|
      marker.lat(location.latitude)
      marker.lng(location.longitude)
      marker.infowindow render_to_string(:partial => "info", :locals => { :event => location })
      marker.picture({
        "url" => ActionController::Base.helpers.image_path('icon-marker-truck.gif'),
        "width" => 28,
        "height" => 40
      })
    end
  end

  def refresh
    render :partial => "welcome/info"
  end

  def new
  end

  def create
    params[:event].parse_time_select! :start
    params[:event].parse_time_select! :end_time
    event.attributes = event_params

    if event.save
      redirect_to event_path(event.start.to_date)
      flash[:notice] = "Event created successfully"
    else
      flash[:alert] = "There was an error creating your event"
      render :new
    end
  end

  def date
    @date ||= params[:id].present? ? Date.parse(params[:id]): Date.today
  rescue ArgumentError
    @date = Date.today
  end
  helper_method :date

  def days
    (date.beginning_of_week(:sunday)..date.end_of_week(:sunday))
  end
  helper_method :days

  def event
    if current_user
      @event ||= current_user.events.build(start: date, end_time: date)
    end
  end
  helper_method :event

  def events
    @events ||= current_user.events.for_day(date)
  end
  helper_method :events

def destroy
  event = current_user.events.find(params[:id])
  event.destroy
  redirect_to event_path(event.start.to_date), :notice => "Your event has been deleted"
end
  private
    def event_params
      params.require(:event).permit(:start, :end_time, :address, :date)
    end

end

1 个答案:

答案 0 :(得分:0)

尝试:

index = 0
events.each do |event|
  # ...
  index += 1 if event.new_record?
end

对我的口味来说有点太多程序,但它应该做的工作