ruby on rails - Search form don't update table in RoR -
i'm trying write filter table. here code:
.../models/message.rb
class message < activerecord::base include filterable belongs_to :user default_scope -> { order('created_at desc') } validates :user_id, presence: true validates :content, presence: { message: "Выберите файл послания для загрузки!" }, file_size: { less_than_or_equal_to: 20.megabytes } validates :fromdate, presence: true validates :tilldate, presence: true mount_uploader :content, contentuploader scope :id, -> (id) {where id: id } scope :tariff, -> (tariff) { tariff: tariff } scope :status, -> (status) { where("status ?", "#{status}%")} end
.../controllers/concerns/filterable.rb
module filterable extend activesupport::concern module classmethods def filter(filtering_params) results = self.where(nil) filtering_params.each |key, value| results = results.public_send(key, value) if value.present? end results end end end
.../controllers/messages_controller.rb
... def index @messages = message.filter(params.slice(:id, :tariff, :status)).paginate(page: params[:page], :per_page => 10) end ...
.../views/messages/index.html.erb
... <table class="table table-bordered"> <thead> <tr> <th>Клиент</th> <th>№</th> <th>Дата</th> <th>Послание</th> <th>Тариф</th> <th>Показ с</th> <th>до</th> <th>Стоимость, р.</th> <th>Статус</th> <% if current_user.admin? %> <th>Модератор</th> <% end %> </tr> </thead> <tbody> <tr> <%= form_tag messages_path, method: "get" %> <td><%= text_field_tag "client" %></td> <td><%= text_field_tag "id", nil, size: 1 %></td> <td></td> <td><%= submit_tag "Выбрать" %><br><br><%= button_tag "Очистить" %></td> <td><%= select_tag "tariff", options_for_select(["", "1. Вечер", "2. Весь день", "1001. ВЕЧЕР БЕСПЛАТНО", "1002. ДЕНЬ БЕСПЛАТНО"]), {} %></td> <td></td> <td></td> <td></td> <td><%= select_tag "status", options_for_select(["", "В обработке", "Исполняется", "Отклонена", "Выполнена"]), {} %></td> <td></td> <% end %> </tr> <%= render @messages %> </tbody> </table> </div> ...
.../views/messages/_message.html.erb
... <% case message.status when "В обработке" @messagecolor="warning" when "Исполняется" @messagecolor="success" when "Выполнена" @messagecolor="info" when /Отклонена.*/ @messagecolor="danger" end unless message.moderator.nil? @moderator = user.find(message.moderator).name end %> <tr class=<%= @messagecolor %> data-message_id="<%= message.id %>"> <% if current_user.admin? || current_user.role == "Модератор" %> <td><%= message.user.name %></td> <% end %> <td><%= message.id %></td> <td><%= message.created_at.strftime("%d/%m/%y %t %z") %></td> <td><img src="<%= message.content_url(:thumb) %>" data-toggle="modal" data-target="#mymodal" data-content="<%= message.content_url%>"></td> <td><%= message.tariff %></td> <td><%= message.fromdate.strftime("%d/%m/%y") %></td> <td><%= message.tilldate.strftime("%d/%m/%y") %></td> <td><%= message.cost %></td> <td class="status"> <%= message.status %> <% if (current_user.admin? || current_user.role == "Модератор") && message.status == "В обработке" %> <br> <div class="btn-group"> <button type="button" class="btn btn-success" data-status="Исполняется" data-message_id="<%= message.id %>">Принять</button> <button type="button" class="btn btn-danger" data-status="Отклонена" data-message_id="<%= message.id %>">Отклонить</button> </div> <% end %> <% if (current_user.admin? || current_user.role == "Модератор") && message.status == "Исполняется" %> <br> <%= link_to "Изменить", edit_message_path(message.id), :class => "btn btn-info" %> <% end %> </td> <% if current_user.admin? %> <td class="moderator"> <%= @moderator %><br> <%= message.updated_at.strftime("%d/%m/%y %t %z")%> </td> <% end %> </tr> ...
the filter works after page refreshed. worth page through menu - form button not work, update f5 - works. why?
i grateful if tell me how save selected filter values in fields after applied.
i found answer main question: need include entire table form in .../views/messages/index.html.erb
... <%= form_tag messages_path, method: "get" %> <table class="table table-bordered"> <thead> <tr> <th>Клиент</th> <th>№</th> <th>Дата</th> <th>Послание</th> <th>Тариф</th> <th>Показ с</th> <th>до</th> <th>Стоимость, р.</th> <th>Статус</th> <% if current_user.admin? %> <th>Модератор</th> <% end %> </tr> </thead> <tbody> <tr> <td><%= text_field_tag "client" %></td> <td><%= text_field_tag "id", nil, size: 1 %></td> <td></td> <td><%= submit_tag "Выбрать" %><br><br><%= button_tag "Очистить" %></td> <td><%= select_tag "tariff", options_for_select(["", "1. Вечер", "2. Весь день", "1001. ВЕЧЕР БЕСПЛАТНО", "1002. ДЕНЬ БЕСПЛАТНО"]), {} %></td> <td></td> <td></td> <td></td> <td><%= select_tag "status", options_for_select(["", "В обработке", "Исполняется", "Отклонена", "Выполнена"]), {} %></td> <td></td> </tr> <%= render @messages %> </tbody> </table> <% end %> </div> ...
and works correctly.
Comments
Post a Comment