【问题标题】:How to filter with multiple parameters in Rails?如何在 Rails 中使用多个参数进行过滤?
【发布时间】:2015-07-07 14:34:26
【问题描述】:

我正在构建一个应用程序,用户可以在其中提交帖子,然后用他们所在的年份、位置和军事单位进行标记。在我的索引页面上,我希望有 3 个用于年份位置和军事单位的下拉菜单,并且当用户在下拉列表中指定不同的值时,索引仅过滤具有这些参数的帖子。但是,我一次又一次地失败了。

这是我的帖子控制器:

    def index
    @posts = Post.all
    @posts = @posts.location(params[:location]) if params[:location].present?
    @posts = @posts.unit(params[:unit]) if params[:unit].present?
    @posts = @posts.year(params[:year]) if params[:year].present?
    end

我的索引视图:

    <h1>All Posts</h1>
    <%= form_tag 'search', method: 'get' do %>
    <%= select :location, options_for_select(countries) %>
    <%= select :year, Time.now.year.downto(1900).to_a %>
    <%= select :unit, options_for_select(["army", "navy"]) %>
    <%= submit_tag "filter" %>
    <% end %>

</p>
<ul id="posts">
<% @posts.each do |post| %>
<li>
<%= link_to post.id, post_path(post) %>
</li>
<% end %>
<%= link_to "New Post", new_post_path %>
</ul>

还有我的帖子模型:

    class Post < ActiveRecord::Base
    belongs_to :user
    has_many :comments
    has_attached_file :image
    validates_attachment_content_type :image, :content_type =>     ["image/jpg","image/jpeg","image/png"]
end

就像现在一样,我收到的错误是“Posts#index 中的 NoMethodError”:“未定义的方法 `empty?'对于零:NilClass”。突出显示的行来自视图:

<%= select :location_id, options_for_select(countries) %>

感谢您对此的任何帮助。我已经搜索过,但没有找到太多有用的东西。

编辑:国家来自以下助手:

    module CountriesHelper


    def countries
    [
     "Afghanistan",
     "Aland Islands",
     "Albania",
     "Algeria",

#truncated for space

      "Western Sahara",
      "Yemen",
      "Zambia",
      "Zimbabwe"
    ]

    end
    end

【问题讨论】:

  • 什么是countries?它来自哪里?
  • 它只是一个国家名称数组的助手。
  • 请使用该帮助代码更新您的帖子。
  • 当然,就是这样做的。
  • 您在&lt;%= select :location_id, options_for_select(countries) %&gt; 这一行发布了错误报告,但您的表单中有&lt;%= select :location, options_for_select(countries) %&gt;。是不是笔误?

标签: ruby-on-rails ruby model-view-controller


【解决方案1】:

选择数组需要这样格式化:

def countries
    [
     ["Afghanistan","Afghanistan"],
     ["Aland Islands","Aland Islands"],
     ["Albania","Albania"],
     and so on
.
.
.
    ]

然后根据您的示例 sn-p 在搜索中使用选择选项:

<%= select_tag :search, options_for_select(countries), prompt: "Choose a country", class: 'your_css_class_here' %>

可能还有其他一些问题,但这会正确设置辅助数组。让我知道发生了什么,我可以添加到我的答案中。

【讨论】:

  • 太棒了!很高兴听到它。
【解决方案2】:

国家变量来自哪里?这是一个 nil,表示它没有设置。

我不是一个超级 ruby​​ 编码器,但这对我来说也向后看。

def index
@posts = Post.all
@posts = @posts.location(params[:location]) if params[:location].present?
@posts = @posts.unit(params[:unit]) if params[:unit].present?
@posts = @posts.year(params[:year]) if params[:year].present?
end

我觉得会比较像

def index
@posts = Post.all
   if params[:location].present?
       @posts = @posts.location(params[:location]) 
   end
   if params[:unit].present?
      @posts = @posts.unit(params[:unit]) 
   end
   if params[:year].present?
      @posts = @posts.year(params[:year])
   end
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-29
    • 1970-01-01
    • 2016-06-11
    相关资源
    最近更新 更多