我对编程和解决一个小问题都是新手。我刚刚用ruby on rails创建了一个数据库。我希望DB的表格尽可能方便用户。默认情况下,表单索引会将新数据放在页面底部。这是不方便的,因为如果用户想快速验证他/她是否正确输入了数据,则用户必须一直滚动到页面底部。有没有一种方法可以更改默认值,以便在索引视图中的表顶部显示新数据。
任何提示都会有所帮助。
def index
@objects = Object.order 'created_at DESC'
end
欢迎来到编程世界!
这将根据创建对象的时间对对象进行排序。将对象替换为模型的名称。可能有一行类似Object.all。
Rails有许多入门指南:http://guides.rubyonrails.org
祝你好运!
让您有一个名为"posts"的表,并使用Post模型。
在post_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