【发布时间】:2015-08-07 02:12:59
【问题描述】:
我是一个尝试实现datagrid gem (https://github.com/bogdan/datagrid) 的新手。不幸的是,我收到了三个错误消息:
Error 1: undefined method 'filter' for #<UsersGrid:0x000000044e9400>
Referring to: line `@grid.filter` in def index
Error 2: undefined local variable or method 'user' for #<UsersController:0x007f821f434808>
Referring to: line `link_to view_context.image_tag("delete.gif",...` in the controller.
Error 3: undefined method `image_tag' for UsersGrid:Class
Referring to: line `link_to image_tag(user.avatar.url,` in users_grid.rb
Placing `view_context` in front of link_to in users_grid.rb doesn't work either: undefined local variable or method 'view_context' for UsersGrid:Class.
Also tried `ActionController::Base.helpers.image_tag`. Although that seems to solve the image_tag issue, I then get the error message: `undefined method 'user_path' for #<ActionView::Base:0x007f821d3115b8>` referring to that same line.
删除上面的所有行,表单就可以工作了:-)
对如何更改以下代码以针对这些错误进行调整有什么建议吗?
我的代码: 安装 gem 后,我创建了 /app/grids/users_grid.rb:
class UsersGrid
include Datagrid
scope do
User.order("users.created_at desc")
end
filter(:id, :integer)
filter(:email, :string) { |value| where('email like ?', "%#{value}%") }
filter(:organization, :string) { |value| where('organization like ?', "%#{value}%") }
filter(:verified, :xboolean)
filter(:created_at, :date, :range => true, :default => proc { [1.month.ago.to_date, Date.today]})
column(:id)
column(:avatar) do |user|
if user.avatar?
link_to image_tag(user.avatar.url, alt: "Profile"), user_path(user) #Error3
else
link_to image_tag("profile.gif", alt: "Profile"), user_path(user)
end
end
column(:organization)
column(:verified) do |user|
image_tag("verifiedaccount.gif", title: "verified") if user.verified
end
column(:created_at) do |model|
model.created_at.to_date
end
end
用户控制器:
def index
@grid = UsersGrid.new(params[:users_grid]) do |scope|
scope.where(admin: false).page(params[:page]).per_page(30)
end
@grid.assets
if (current_user && current_user.admin?) # This is a Sessions helper method.
@grid.filter(:activated, :xboolean, :default => true) #Error1
@grid.column(:myadmin, :header => "My admin?") do |user|
view_context.image_tag("adminamina.png", title: "admin") if user.myadmin
end
@grid.column(:activated) do |user|
user.activated_at.strftime('%v') if user.activated
end
@grid.column(:remove) do |user|
link_to view_context.image_tag("delete.gif", title: "remove"), user, method: :delete,
data: { confirm: "Please confirm" } #Error2
end
end
end
观点:
<%= datagrid_form_for @grid, :method => :get, :url => users_path %>
<%= will_paginate(@grid.assets) %>
<%= datagrid_table(@grid) %>
<%= will_paginate(@grid.assets) %>
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-4 datagrid