所以我试图让用户从我认为的链接中对一组食谱进行排序:
<%= 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
这个想法是用排序列表重定向到索引视图并只渲染视图。我没有收到任何错误,但是当我单击链接时,页面重新加载但没有任何反应。
食谱类如下所示:
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
我在这里做错了什么?