Mysql Rails如何修复表以获取任何url
在my Records index.html.erb中,我有一个带有链接标题的表,以便该表能够自行排序:Mysql Rails如何修复表以获取任何url,mysql,ruby-on-rails,model-view-controller,rails-activerecord,Mysql,Ruby On Rails,Model View Controller,Rails Activerecord,在my Records index.html.erb中,我有一个带有链接标题的表,以便该表能够自行排序: <table class="table table-hover" id="records"> <thead> <tr> <th><%= sortable "firstname", "Frist Name"%></th> <th><%= sortable "lastna
<table class="table table-hover" id="records">
<thead>
<tr>
<th><%= sortable "firstname", "Frist Name"%></th>
<th><%= sortable "lastname", "Last Name" %></th>
...
这两者的最终产品是HTML页面上的以下代码:
<th><a class="btn btn-default btn-xs" href="/records?direction=asc&sort=lastname">Last Name</a></th>
我的记录模型:
def self.search(search)
if search
results = where('firstname LIKE ?', "%#{search}%")
if !results.blank?
results
else
results = where('lastname LIKE ?', "%#{search}%")
if !results.blank?
results
else
results
end
end
else
Record.all
end
end
我找错地方了。在我的记录控制器的更深处,我忘记了:
def sort_column
Record.column_names.include?(params[:sort]) ? params[:sort] : "name"
end
def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
end
我所要做的就是把“名字”换成“名字”,这就为我解决了这个问题
def index
if current_user.profile.title == "admin"
@records = Record.order(sort_column + " " + sort_direction)
else
if params[:search].blank? || params[:search] ==''
@records = Record.where(loanofficer_id: current_user.id).order(sort_column + " " + sort_direction)
else
@records = Record.search(params[:search])
end
end
end
def self.search(search)
if search
results = where('firstname LIKE ?', "%#{search}%")
if !results.blank?
results
else
results = where('lastname LIKE ?', "%#{search}%")
if !results.blank?
results
else
results
end
end
else
Record.all
end
end
def sort_column
Record.column_names.include?(params[:sort]) ? params[:sort] : "name"
end
def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
end