Answer the question
In order to leave comments, you need to log in
How to implement Ajax search in Rails?
Hello, I have the following code
<%= form_tag ads_path, :method => 'get' do %>
<%= label_tag("Поиск по тексту:") %>
<%= text_field_tag :search, params[:search] %>
<br/>
<a class="btn btn-success"><%= submit_tag 'найти', :name => nil %></a>
<% end %>
Answer the question
In order to leave comments, you need to log in
render was a bug that doesn't work in Assets right now.
action.js.erb
, then everything works fine, see why it doesn’t work for you.
Read for understanding.
And then see a similar question.
index.html.erb
<%= form_tag ads_path, :method => 'get', :id => "ads_search" do %>
<%= label_tag("Поиск по тексту:") %>
<%= text_field_tag :search, params[:search] %>
<br/>
<a class="btn btn-success"><%= submit_tag "Найти", :name => nil %></a>
<% end %>
<br/>
<div id="ads">
<%= render 'ads' %>
</div>
<%= will_paginate @posts%>
<h1>Список объявлений</h1>
<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>
class Ad < ActiveRecord::Base
belongs_to :city
attr_accessible :title, :text, :number, :published_date, :city_id
def self.search(search)
if search
where('text LIKE ?', "%#{search}%")
else
Ad.all
end
end
end
def index
@ads = @ads.paginate(:page => params[:page], :per_page => 50).order("created_at desc").search(params[:search])
end
//= require jquery
//= require jquery_ujs
//= require_tree .
$(function () {
// Search form.
$('#ads_search input').keyup(function () {
$.get($('#ads_search').attr('action'), ↵
$('#ads_search').serialize(), null, 'script');
return false;
});
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question