【问题标题】:Rails: trouble sorting array from viewRails:从视图中排序数组时遇到问题
【发布时间】:2013-02-22 12:16:48
【问题描述】:

所以我试图让用户从我认为的链接中对一组食谱进行排序:

<%= link_to "Score", recipes_sort_path, :order => 'score' %>

我将参数“score”发送到我的控制器方法“sort”,如下所示:

  def sort
    if (params[:order] == 'score')
      @recipes.sort_by(&:score)
    end

    respond_to do |format|
      format.html { redirect_to recipes_path }
      format.json { render json: @recipe }
    end
  end

它重定向到以下索引方法:

  def index
    # If recipes already present, skip following
    if (!@recipes)
      if (params[:search] || params[:tag])
        @recipes = Recipe.search(params[:search], params[:tag])
      else
        @recipes = Recipe.all
      end
    end

    respond_to do |format|
      format.html 
      format.json { render json: @recipe }
    end
  end

这个想法是用排序列表重定向到索引视图,然后只渲染视图。 我没有收到任何错误,但是当我单击链接时,页面会重新加载但没有任何反应。

Recipe 类如下所示:

class Recipe < ActiveRecord::Base
  attr_accessible :instructions, :name, :slug, :score, :upvotes, :downvotes, :comments, :image

  has_and_belongs_to_many :ingredients
  has_and_belongs_to_many :tags
  has_many :comments  
  belongs_to :user
  delegate :name, :to => :user, :prefix => :user, :allow_nil => true
  mount_uploader :image, ImageUploader

  validates :name, :presence => true

  def score
    score = (self.upvotes - self.downvotes)
  end
end

我在这里做错了什么?

【问题讨论】:

    标签: ruby-on-rails arrays sorting view


    【解决方案1】:

    还有第三个选项(前两个来自 ckruse 的回答)。您可以从排序操作呈现索引模板

    def sort
      if (params[:order] == 'score')
        @recipes.sort_by!(&:score)
      end
    
      respond_to do |format|
        format.html { render :index }
        format.json { render json: @recipe }
      end
    end
    

    这将在排序操作中使用@recipes 时使用索引模板。您还保存了一个请求,因为您没有重定向。

    我想评论的另一件事是链接。应该是

    <%= link_to "Score", recipes_sort_path(:order => 'score') %>
    

    更新:获取@recipes。尽可能地,我希望 sql 进行排序,这就是我要在这里做的事情。

    def sort
      @recipes = Recipe
    
      if params[:order] == 'score'
        @recipes = @recipes.order('upvotes - downvotes')
      end
    
      respond_to do |format|
        format.html { render :index }
        format.json { render json: @recipe }
      end
    end
    

    【讨论】:

    • @recipes 在您的示例中仍然为空白。
    • 我以为你很清楚如何获取@recipes 并按分数对其进行排序。您只是无法显示已排序的 @recipes。给我一分钟更新我的答案。
    • 恐怕OP不清楚。他似乎是编程行业的新手(对不起:)
    • 是的,至少在这个框架中是新的,谢谢。我使用了您的排序方法示例。它有效,感谢您的时间。
    【解决方案2】:

    首先:sort_by 不是“破坏性的”,它返回一个新数组。您可能想使用sort_by! 或将sort_by 的返回值保存到@recipes

    第二:您根本不会在 sort 操作中渲染任何内容。如果您发布了所有代码,即使@recipes 也是空的。你可以做两件事:

    • sort 方法中检索数据,就像在 index 方法中所做的那样,然后调用 render :index
    • 在您的index 方法中排序,根本不要使用sort 方法。您可以将多个 URI 路由到同一个操作。

    【讨论】:

    • 好的,好吧,我摆脱了排序方法,但我仍然做错了什么。我将排序放在索引方法中:' if (params[:order] == 'score') @recipes.sort! { |r| r.score } end # 渲染视图 '
    • 您是否使用正确的参数调用您的操作?例如。 http://localhost:3000/recipes?order=score?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-10
    • 2013-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    相关资源
    最近更新 更多