D
D
dexdev2014-01-18 17:31:44
Ruby on Rails
dexdev, 2014-01-18 17:31:44

How to make selection filters in rails?

In general, I’ve been sitting for 4 hours now and I can’t figure out how to make a selection list for users ...
The bottom line: There are posts with status and cities and categories, now index.html.erb displays all posts for all cities and statuses, but you need to do so that you can was to select posts by city and by status. For example, in a drop-down list or cheb box, you could select the city and post status.
and how can the user by default see the posts of the city selected during registration?
Post Model

class Post < ActiveRecord::Base

  NORMAL    = 1
  ACTIVE    = 2
  COMPLETED = 3

  STATUSES = {
    NORMAL    => 'не опубликовано',
    ACTIVE    => 'опубликовано',
    COMPLETED => 'помечено для удаления'
  }

  def status_name
    STATUSES[status]
  end

  belongs_to :user
  belongs_to :category
  belongs_to :city
  default_scope -> { order('created_at DESC') }

end

class PostsController < ApplicationController

def index
    @category = Category.find(params[:category_id])
    @feed_items = @category.posts.paginate(page: params[:page])
  end
end

posts/index.html.erb
<h1><%= @category.name %></h1>
<%= link_to 'Все категории', all_post_path %>
<% Category.all.each do |category| %>
<%= link_to category.name, category_posts_path(category) %> 
<% end %>

<%= render 'categories/feed' %>

categories/_feed.html.erb
<% if @feed_items.any? %>

    <%= render partial: 'categories/feed_item', collection: @feed_items %>

  <%= will_paginate @feed_items %>
<% end %>

categories/feed_item.html.erb
<%= link_to (truncate feed_item.name,:length => 35),	feed_item %>
  	
<%= link_to feed_item.user.name,	(feed_item.user) %>

or tell me in which direction to google ... otherwise it’s not even clear how to search
Yuzai rails 4
ruby ​​2

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FanKiLL, 2014-01-18
@FanKiLL

Make through collection_select send to the server on button click or ajax as it is more convenient.
Add the user's choice to the request

@feed_items = @category.posts.where(status: params[то что придёт от юзера]).paginate(page: params[:page])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question