如何订购下拉菜单,但在其中随机化?

时间:2021-02-24 11:20:19

How can I change this:

我怎么能改变这个:

  def index
    @values = Value.all
  end

... to something that will put my drop down menu categories in this order on the index:

...以某种方式将我的下拉菜单类别按此顺序放在索引上:

1) Mantra, 2) Quote, 3) Principle, 4) Other

1)Mantra,2)引用,3)原则,4)其他

BUT within each category the Value would be randomized, for example Mantra will show up first in the index but within the mantra category the mantras you typed in will have their order randomized upon each page reload.

但是在每个类别中,值将被随机化,例如,Mantra将首先出现在索引中,但在咒语类别中,您输入的咒语将在每次重新加载页面时随机化其顺序。

value.rb

value.rb

class Value < ActiveRecord::Base
	belongs_to :user

	VALUES = ['Mantra', 'Quote', 'Principle', 'Other']

end

values_controller.rb

values_controller.rb

class ValuesController < ApplicationController
  before_action :set_value, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!, except: [:index, :show]

  def index
    @values = Value.all
  end

  def show
  end

  def new
    @value = current_user.values.build
  end

  def edit
  end

  def create
    @value = current_user.values.build(value_params)
    if @value.save
      redirect_to @value, notice: 'Value was successfully created'
    else
      render action: 'new'
  end
end

  def update
    if @value.update(value_params)
      redirect_to @value, notice: 'Value was successfully updated'
    else
      render action: 'edit'
  end
end

  def destroy
    @value.destroy
    redirect_to values_url
  end

  private
    def set_value
      @value = Value.find(params[:id])
    end

    def correct_user
      @value = current_user.values.find_by(id: params[:id])
      redirect_to values_path, notice: "Not authorized to edit this value" if @value.nil?
    end

    def value_params
      params.require(:value).permit(:name, :categories)
    end
end

index.html.erb

index.html.erb

<div id="values" class="transitions-enabled">
  <% @values.each do |value| %>
        <%= value.name %><br/>
        <% if value.user == current_user %>
          <div class="actions">
            <%= link_to edit_value_path(value) do %>
            <b><%= value.categories %></b>
              <span class="glyphicon glyphicon-edit"></span>
            <% end %>
            <%= link_to value, method: :delete, data: { confirm: 'Are you sure?' } do %>
              <span class="glyphicon glyphicon-trash"></span>
            <% end %>
          </div>
        <% end %>
  <% end %>
</div>

1 个解决方案

#1


1  

Problem 1: order the @values in a custom order:

问题1:按自定义顺序排序@values:

1) Mantra, 2) Quote, 3) Principle, 4) Other

1)Mantra,2)引用,3)原则,4)其他

I'm assuming these labels come from the name attribute of your Value model. To get this non alphabetic custom ordering, and not having to rely on the created_at field for the ordering, you need a different attribute to sort on. I'd add a sort:integer attribute to Value and update the records with values for the sort field:

我假设这些标签来自您的Value模型的name属性。要获得这种非字母自定义排序,而不必依赖created_at字段进行排序,您需要使用不同的属性进行排序。我将一个sort:integer属性添加到Value并使用sort字段的值更新记录:

Value: name: "Mantra", sort: 0
Value: name: "Quote", sort: 1
Value: name: "Principle", sort: 2
Value: name: "Other", sort: 3

Then change your query to:

然后将您的查询更改为:

@values = Value.order :sort

That will get you the ordering you specified. Now, for your next problem. I'm assuming you have some association on this Value model. You mentioned:

这将为您提供您指定的订单。现在,为你的下一个问题。我假设你在这个Value模型上有一些关联。你提到过:

BUT within each category

但在每个类别中

So each Value instance is a category? If you have other models that belong_to a category, you would have a separate query that you would do the ordering on:

那么每个Value实例都是一个类别?如果您有其他属于某个类别的模型,您将拥有一个单独的查询,您可以对其进行排序:

# index action
@categories = Category.order :sort

# then user clicks on a category and they go to its
# show action
@category = Category.find params[:id]
@items = @category.items.order 'RANDOM()'

So with the above, at your category index page would see the categories sorted by your custom ordering then, upon clicking on a category (e.g Mantra) to navigate to its show action they'd see items of the Mantra category in a random order on each page load.

因此,使用上面的内容,在您的类别索引页面上会看到按您的自定义排序排序的类别,然后在点击某个类别(例如Mantra)导航到其显示操作时,他们会以随机顺序查看Mantra类别的项目每页加载。

#1


1  

Problem 1: order the @values in a custom order:

问题1:按自定义顺序排序@values:

1) Mantra, 2) Quote, 3) Principle, 4) Other

1)Mantra,2)引用,3)原则,4)其他

I'm assuming these labels come from the name attribute of your Value model. To get this non alphabetic custom ordering, and not having to rely on the created_at field for the ordering, you need a different attribute to sort on. I'd add a sort:integer attribute to Value and update the records with values for the sort field:

我假设这些标签来自您的Value模型的name属性。要获得这种非字母自定义排序,而不必依赖created_at字段进行排序,您需要使用不同的属性进行排序。我将一个sort:integer属性添加到Value并使用sort字段的值更新记录:

Value: name: "Mantra", sort: 0
Value: name: "Quote", sort: 1
Value: name: "Principle", sort: 2
Value: name: "Other", sort: 3

Then change your query to:

然后将您的查询更改为:

@values = Value.order :sort

That will get you the ordering you specified. Now, for your next problem. I'm assuming you have some association on this Value model. You mentioned:

这将为您提供您指定的订单。现在,为你的下一个问题。我假设你在这个Value模型上有一些关联。你提到过:

BUT within each category

但在每个类别中

So each Value instance is a category? If you have other models that belong_to a category, you would have a separate query that you would do the ordering on:

那么每个Value实例都是一个类别?如果您有其他属于某个类别的模型,您将拥有一个单独的查询,您可以对其进行排序:

# index action
@categories = Category.order :sort

# then user clicks on a category and they go to its
# show action
@category = Category.find params[:id]
@items = @category.items.order 'RANDOM()'

So with the above, at your category index page would see the categories sorted by your custom ordering then, upon clicking on a category (e.g Mantra) to navigate to its show action they'd see items of the Mantra category in a random order on each page load.

因此,使用上面的内容,在您的类别索引页面上会看到按您的自定义排序排序的类别,然后在点击某个类别(例如Mantra)导航到其显示操作时,他们会以随机顺序查看Mantra类别的项目每页加载。