LoginSignup
0
0

More than 5 years have passed since last update.

userlistを並べ変える方法

Last updated at Posted at 2017-10-26

controllersの users_controlller.rbの
先頭付近に以下を記述する

helper_method :sort_column, :sort_direction
def index
  @users = User.all.order(sort_column + ' ' + sort_direction)
end

そして、helpersのusers_helper.rbの最後付近に
以下を記述する。

css_class = (column == sort_column) ? "current #{sort_direction}" : nil
direction = (column == sort_column && sort_direction == "asc") ? "desc" : "asc"
link_to title, {:sort => column, :direction => direction}, {:class => css_class}

また、usersのindex.html.slimの table.ui.celled.padded.table の下に

th
  = sortable "id", "Id"
th
  = sortable "account","Account"
th
  = sortable "username", "Username"
th
  = sortable "email", "E-mail"
th
  = sortable "created", "Created"

以上のように記述する
  
そして、@users.each do |user| に下に

td
  = user.id
td
  = user.account
td
  = user.name
td
  = user.email
td
  = user.created_at

のように記述すると
id account name email created の降順 、昇順で並び替えができる。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0