如果控制器为零,则将默认值设置为控制器中的param属性?
我允许用户按created_at
和total_votes
对post进行排序:
posts_controller.rb:
@posts = current_user.subscribed_posts.paginate(:page => params[:page], :per_page => 5, :order => params[:order_by])
show.html.erb:
<span class="comment-tab current-tab"> "created_at DESC") %> <span class="comment-tab current-tab" > "created_at ASC") %> <span class="comment-tab current-tab"> "total_votes DESC") %>
我想:order_by
默认具有total_votes DESC
的值(当它为nil时)。
这样做的最佳方法是什么?
在控制器中
params[:order_by] ||= "total_votes DESC"
要么
# fetch returns the default value if the key is not present params.fetch(:order_by, "total_votes DESC")
哈希#取