我正在尝试为我的webapp实现一个简单的搜索和排序.我正在关注railscast和这个railscast.
我作为链接使用的可排序函数的应用程序帮助程序是:
def sortable(column, title = nil)
title ||= column.titleize
css_class = column == sort_column ? "current #{sort_direction}" : nil
direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"
link_to title, params.merge(:sort => column, :direction => direction, :page => nil), {:class => css_class}
end
Run Code Online (Sandbox Code Playgroud)
我在视图中使用这些.在控制器中我使用白名单:
@listingssearch.where(:vehicletype => 'Car').order(sort_column + " " + sort_direction).paginate(:page => params[:page], :per_page => 30)
Run Code Online (Sandbox Code Playgroud)
私人消毒方法:
private
def sort_column
Listing.column_names.include?(params) ? params[:sort] : "rateperhour"
end
def sort_direction
%w[asc desc].include?(params[:direction]) …Run Code Online (Sandbox Code Playgroud) 我有一个 rails 应用程序,我在其中使用 devise 进行身份验证。模型是用户。我还为管理仪表板使用了活动管理员并在其中创建了用户资源。用户在管理端有编辑、查看和删除链接,在普通用户表单上有一个编辑表单(由设计提供)。
我想让管理员用户无需知道他们的密码就可以在活动的管理员仪表板上更改用户的详细信息。这意味着在活动管理端没有验证用户编辑。我应该如何处理?
我的 admin/user.rb 看起来像:
ActiveAdmin.register User do
active_admin_importable
# See permitted parameters documentation:
# https://github.com/activeadmin/activeadmin/blob/master/docs/2-resource-customization.md#setting-up-strong-parameters
#
permit_params :email, :name, :role, :zipcode, :city, :street_address, :state, :phone_number, :password, :password_confirmation, :leads2dealscustomer, :slug, :verified,:tdcfinance ,:textcolor
form do |f|
f.inputs "User" do
f.input :email
f.input :name
f.input :password
f.input :password_confirmation
f.input :role
f.input :street_address
f.input :city
f.input :state
f.input :zipcode
f.input :phone_number
end
f.actions
end
scope :all_users
scope :basic_users
scope :basic_dealers
scope :basic_repairshops
scope :silver_dealers
scope :silver_repairshops
scope :gold_dealer
scope …Run Code Online (Sandbox Code Playgroud) 我正在一个项目中,我有一个import_listings(file)由Sidekiq perform方法调用的CSV上传器类方法。
class CsvWorker
include Sidekiq::Worker
sidekiq_options retry: false
def perform(file)
#call import listings class method on the file.
imported = Listing.import_listings(file)
# return true
end
end
Run Code Online (Sandbox Code Playgroud)
我在控制器动作索引中调用perform_async,如下所示:
def import
@imported = CsvWorker.perform_async(params[:file].path)
end
Run Code Online (Sandbox Code Playgroud)
一切正常!
我想在后端的活动管理仪表板页面中移动此方法。如下图所示。会有用于不同CSV上传的列,每个列都有一个文件上传表格:
成员操作将最不适合此任务,因为没有特定的资源。这是一个仪表板页面。
仪表板的代码如下:
ActiveAdmin.register_page "Dashboard" do
menu priority: 1, label: proc{ I18n.t("active_admin.dashboard") }
content title: proc{ I18n.t("active_admin.dashboard") } do
div class: "blank_slate_container", id: "dashboard_default_message" do
span class: "blank_slate" do
span "Welcome To Want A Car Buy A Car Backend!"
small "Use options below …Run Code Online (Sandbox Code Playgroud) 通常在回溯中,我们采用一个辅助函数,它接受一个初始状态,每个递归调用负责自己的计算并将结果传递给下一个递归调用.从理论上讲,我们通过看不见和看到的变量来表示这一点.
例如,在字符串的排列中,我们将使用此程序:
def permute(str)
return str if str.length < 2
permute_helper(str, "")
end
def permute_helper(unseen, seen)
#base case
if unseen.length <= 0
p seen
return
else
(0..unseen.length-1).each do |i|
buffer = unseen
buffer = buffer.split('')
buffer.delete_at(i)
buffer = buffer.join('')
permute_helper(buffer, seen+unseen[i])
end
end
end
permute('abc')
Run Code Online (Sandbox Code Playgroud)
你会打印出所需的结果.
在最近的一次采访中,我被要求在不使用两个变量的情况下这样做.没有在看到的变量中存储状态.当时我无法全脑思考,但我想问一下如何在不存储状态的情况下进行回溯?
ruby ×2
activeadmin ×1
algorithm ×1
backtracking ×1
csv ×1
dashboard ×1
devise ×1
parameters ×1
recursion ×1
search-form ×1
sidekiq ×1
sorting ×1
validation ×1