D
D
Danila Zaitsev2014-10-31 05:55:47
JavaScript
Danila Zaitsev, 2014-10-31 05:55:47

How to display records with a specific value (from select)?

There are 2 entities:

class Ad < ActiveRecord::Base
  belongs_to :city
  attr_accessible :title, :text, :number, :published_date, :city_id
end

class City < ActiveRecord::Base
  has_many :ads
  attr_accessible :name, :ad_id  
end


and there is a view
<h1>Список объявлений</h1>

<%= link_to 'Новое объявление', new_ad_path %><br/><br/>

<%= label :city, 'Город: ' %>
<%= collection_select(:ad, :city_id, City.all, :id, :name, prompt: 'Выберете город...') %><br/>

<br/>
<table>
<% @ads.each do |ad| %>
    <h2><%= link_to ad.title, ad %></h2>
    <%= ad.text %><br/>
    <strong>Город: </strong><%= ad.city.name %><br/>
    <strong>Дата публикации: </strong><%= ad.created_at %>
<% end %>
</table>

<br/>
<%= will_paginate @posts%>

I can’t figure out how to make it so that when a specific field is selected in the collection_select, it will issue records below only with this city. I watched 88 screencasts of dynamic selects, it says how to derive from one another, and basically everything I found, there were selects from selects everywhere. Can you provide an example or help me find what I'm looking for? Thanks in advance to everyone who will respond.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
Jeiwan, 2014-10-31
@Jukeboxx

1. We send a request when an element is selected from the select:
The request goes to the ads_path (that is, the index action of the ads controller), the city id is passed in the ad[city_id] parameter.
2. In the action, return the list of city announcements:

respond_to :json
def index
  @city = City.find(params[:ad][:city_id])
  respond_with @ads = @city.ads
end

3. On js we catch the server response with data:
$ ->
  $("#ad_city_id").on "ajax:success", (e, data, status, xhr) ->
    objects = xhr.responseJSON

The objects variable will contain declarations that we then render to the page. That is, all work on displaying ads on the page is transferred to the client, i.e. js. It is very convenient to do this using the handlebars_assets gem and, accordingly, templates in the handlebars format. You can return beautiful json using the jbuilder gem.

B
Boniface, 2014-10-31
@Boniface

Luckily I'm not familiar with Rails. But on angular.js, this is done elementarily thanks to bindings. I recommend looking in his direction. As far as I remember, rail has a REST-only Api project with truncated controllers and without AcriveRecord. Pay attention to it.
Specifically for your situation. When you select a city in the select field, you will have to make a request to the server to get cities and display them as you are doing now. If the relationship is 1:m or just display the value if 1:1. And of course, when loading by default the city in select, you should already have the associated entry pulled up.
Good luck!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question