使用复选框和haml存档条目

时间:2015-04-16 09:53:59

标签: ruby-on-rails ruby mongoid haml

我有一个控制器,用户可以保存用户条目,管理员可以更新。有一个索引页面,显示已保存和现有的条目,一个显示单个条目的显示页面和一个可以更新该条目的编辑页面。我现在要做的是在索引页面上。我希望这里的条目每个都有一个复选框,管理员可以选择这些条目中的哪些归档'。就目前而言,仅仅是在能够清理索引视图的意义上存档这些条目的情况我不想拥有单独的存档控制器等但是在我的诊断控制器中执行此操作以清除我已经说过索引视图,无论如何都不需要在这个阶段进一步完成归档条目。我按照复选框和列表上的轨道进行了操作,但这对我不起作用,我得到了一个" Mongoid :: Errors :: DocumentNotFound"错误。因此我仍然在我的视图和控制器中使用此代码,但此时我已将其评论出来。我沿着创建自定义路线的路线,但我认为这可能是我的问题来自哪里,因为这似乎不起作用。我也在尝试使用TDD和Capybara。如果有人能够建议我接下来应该做什么,那将非常感激。我可以通过复选框和保存按钮显示,但从控制器的角度来看,我似乎在这里碰到了一条墙!

索引

%h1 Diagnostics

%table
  %tr
    %th
    %th User
    %th Message
    %th Device
    %th RELS-Mobile Version
    %th Submitted
    %th Archive
  - @diagnostics.each do |diagnostic|
    %tr
      %td
        %strong= link_to 'show', admin_diagnostic_path(diagnostic)
      %td
        - if diagnostic.user
          = link_to diagnostic.user.email, [:admin, diagnostic.user]
        - else
          unknown
      %td
        = diagnostic.data["message"]
      %td
        %pre= JSON.pretty_generate(diagnostic.data["device"])
      %td
        = diagnostic.data["appVersion"]
      %td
        = diagnostic.updated_at
      %td 
        = check_box_tag 'A Checkbox', method: :put, data: { confirm: "Are you sure?" }

/ = form_tag '/admin/diagnostics/:id/complete', :method => :put do
/   = field_set_tag do
/     = check_box_tag :diagnostic_id, 'diagnostic_ids[]', method: :put, data: { confirm: "Are you sure?" }
/     = submit_tag 'Mark as Complete'

= submit_tag 'Save', method: :put, data: { confirm: "Are you sure?" }
= paginate @diagnostics

控制器

class Admin::DiagnosticsController < Admin::BaseController
  before_filter :diagnostic, :except => [:index]

  def index
    @diagnostics = DiagnosticInfo.all.order_by(:created_at.desc).page(params[:page]).per(50)
  end

  def show
    respond_to do |format|
      format.html
      format.json { render json: @diagnostic }
    end
  end

  def update
    if @diagnostic.update_attributes(params[:diagnostic_info])
      redirect_to admin_diagnostic_path, notice: 'Successfully updated.'
    else
      render action: "edit"
    end
  end

  def edit
  end

  def destroy
    diagnostic.destroy
    redirect_to admin_diagnostics_path
  end

  # def complete
  #  DiagnosticInfo.update_all(["Archive=?", Time.now], :id => params[:diagnostic_ids])
  #  params[:diagnostic_ids]
  #  # redirect_to admin_diagnostics_path
  # end

  private

  def diagnostic
    @diagnostic = DiagnosticInfo.find(params[:id])
  end
end

路线(相关部分)

resources :diagnostics 
#   member do
#     put 'complete'
#   end
# end

功能/规格(相关部分)

  it "can archive a diagnostic report" do 
    diagnostic_info = FG.create(:diagnostic_info)
    visit admin_diagnostics_path
    page.should have_content("test message")
    # save_and_open_page-launchy
    click_button "Save"
    check('A Checkbox')
  end

模型

class DiagnosticInfo
  include Mongoid::Document
  include Mongoid::Timestamps

  field :data, type: Hash
  field :user_id, type: Integer
  field :notes, type: String
  validates :data, presence: true
  #archived @

  index created_at: 1

  def user
    @user ||= User.where(auth_system_user_id: self.user_id).first
  end
end

1 个答案:

答案 0 :(得分:0)

您的问题与this解决方案类似。你可以尝试一下,如果你想要任何不同的话,可以发帖。