使用复选框和 haml 归档条目
Archiving entries with checkbox and haml
我有一个控制器,其中保存了用户条目并且可以由管理员更新。有一个显示已保存和现有条目的索引页面,一个显示单个条目的显示页面和一个可以更新条目的编辑页面。我现在想做的是在索引页上。我希望这里的每个条目都有一个复选框,管理员可以在其中 select 将其中的哪些 'archive'。现在,因为这只是在能够清除索引视图的意义上归档这些条目的情况,我不想有一个单独的归档控制器等,而是在我的诊断控制器中这样做,作为清除索引视图的一种方式正如我所说的那样,索引视图在这个阶段无论如何都不需要对存档的条目做进一步的处理。我遵循了复选框和列表上的 rails 转换,但这对我不起作用,我得到了 "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
features/spec(相关部分)
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
您的问题看起来与 this 解决方案相似。你可以试试看,post 如果你想要不同的话。
我有一个控制器,其中保存了用户条目并且可以由管理员更新。有一个显示已保存和现有条目的索引页面,一个显示单个条目的显示页面和一个可以更新条目的编辑页面。我现在想做的是在索引页上。我希望这里的每个条目都有一个复选框,管理员可以在其中 select 将其中的哪些 'archive'。现在,因为这只是在能够清除索引视图的意义上归档这些条目的情况,我不想有一个单独的归档控制器等,而是在我的诊断控制器中这样做,作为清除索引视图的一种方式正如我所说的那样,索引视图在这个阶段无论如何都不需要对存档的条目做进一步的处理。我遵循了复选框和列表上的 rails 转换,但这对我不起作用,我得到了 "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
features/spec(相关部分)
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
您的问题看起来与 this 解决方案相似。你可以试试看,post 如果你想要不同的话。