我是编程和尝试解决一个小问题的新手。我刚刚在rails上的ruby中创建了一个数据库。我希望我的DB表单尽可能方便用户使用。默认情况下,表单索引会将新数据放在页面底部。这是不方便的,因为如果用户想要快速验证他/她已正确输入数据,则用户必须一直滚动到页面的底部。有没有办法可以更改默认值以在索引视图中的表格顶部显示新数据。
任何提示都会有所帮助。
答案 0 :(得分:0)
def index
@objects = Object.order 'created_at DESC'
end
欢迎来到编程世界!
这会在创建对象时对其进行排序。用您调用的模型替换Object。可能在某处就像Object.all一样。
Rails有很多指南可以帮助您入门:http://guides.rubyonrails.org
祝你好运!答案 1 :(得分:0)
让您有一个名为'posts'的表格,并且正在使用Post模型。
在posts_controller.rb
中def index
#@posts = Post.all
#if we are applying the above query it will fetch all the posts order by id in ascending order.
# you want to show the post which has been created recently. for that you need to change the order of your query. @GoGoCarl has suggested the same taking created_at in account. We can also achieve it taking id field as well.
@posts = Post.order("id DESC")
# This above syntax execute
# SELECT `posts`.* FROM `posts` ORDER BY id DESC. Now you ca get your way out.
end