如何在视图中选择用户集合

时间:2014-01-18 18:38:12

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4

在我的应用程序中我希望向用户显示一些帖子,但帖子有城市,我需要做什么,显示来自选定城市的帖子,用户选择一些城市和视图中显示所选城市的所有帖子? 文件Post.rb

class Post < ActiveRecord::Base

    NORMAL    = 1
  ACTIVE    = 2
  COMPLETED = 3

    STATUSES = {
    NORMAL    => 'unpublished',
    ACTIVE    => 'published',
    COMPLETED => 'deleted'
  }

  def status_name
    STATUSES[status]
  end

    belongs_to :user
    belongs_to :category
  belongs_to :city


  default_scope -> { order('created_at DESC') }

end

文件Posts_controller.rb

class PostsController < ApplicationController
   def index
       @category = Category.find(params[:category_id])
    @feed_items = @category.posts.where(status: params[:status], city_id: params[:city_id]).paginate(page: params[:page])
    end
end

file posts / index.html.erb

<% provide(:title, 'all posts') %>

<div class="container">
<div class="row post_feed">
    <h1><%= @category.name %></h1>
    <div class="col-md-3 col-lg-3" style="padding:0;">
        <ul class="list-group">
    <li class="list-group-item"><%= link_to 'all categories', all_post_path %> </li>
<% Category.all.each do |category| %>
<li class="list-group-item"><%= link_to category.name, category_posts_path(category) %> 
</li>
<% end %>
</ul>
</div>
<div class="col-lg-8">
                <div class="all_post_feed">
                <ul>
                    <div class= "city_select">
                    <%= collection_select(:post, :city_ids, City.all, :id, :name) %>
                    </div>
<%= render 'categories/feed' %>
</ul>
                </div>
            </div>
    </div>
</div>

档案类别/ _feed.html.erb

<% if @feed_items.any? %>

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

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

file categories/_feed_item.html.erb

<li>
    <div class="row post_name" >
        <div class="col-md-8 ">
            <h3>
<%= link_to (truncate feed_item.name,:length => 35),    feed_item %>
</h3>
        </div>
        <div class="col-md-4 preview-small">
            <div class="feed_price pull-right">
<% if feed_item.price.to_f == 0 %>
                price
                <% else %>
                <%= number_to_currency feed_item.price %>
            <% end %>
            </div>
        </div>
    </div>
    <div class="row post_user">
        <div class="col-md-5"> 
            <div class="row">
                <div class="col-md-2 user_avatar">
            <%= avatar_for feed_item.user, :size => "50x50" %>
            </div>
            <div class="col-md-5">
<%= link_to feed_item.user.name,    (feed_item.user) %><br>
<% if feed_item.user.reviews.count.to_f == 0 %>

                        <% else %>
                    <%= (feed_item.user.reviews.count) %>
                    <% end %>
                    </div>
            </div>
        </div>
        <div class="col-md-7 adress">
            <span class="pull-right">
                <%= feed_item.status_name %>
                    <%= l feed_item.date, format: :long %> г.</span><br><span class="pull-right">
                    <%= (truncate feed_item.adress1,:length => 90) %></span><br>
    <span class="pull-right">published <%= time_ago_in_words(feed_item.created_at) %> назад.

    </span>

        </div>
    </div>
</li>

当用户选择城市铁路显示所有来自所选城市的帖子时

<%= collection_select(:post, :city_ids, City.all, :id, :name) %>

1 个答案:

答案 0 :(得分:0)

我用这个

<%= form_for :posts, url: category_posts_path, :html => { :method => 'GET' } do |f| %>
        <%= select_tag :city_id, options_from_collection_for_select(City.all, :id, :name, params[:city_id]), include_blank: true %>
            <% end %>

    <script>
    $(document).ready(function(){
      $('#city_id').change(function(){
        $('form').submit();
      });
        $('#status').change(function(){
        $('form').submit();
         });
    });
    </script>