1

I've a controller for WeekDay. Then I tried to add RanSack in my project. It works good except when I tried to enable remote true, it shows only search form. AJAX call is being made, but nothing shows up.

Controller code

def index
  @q = WeekDay.ransack(params[:q])
  @week_days = @q.result().page(params[:page]).per(10)

  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @week_days }
  end
end

index.html.erb

<div id="week_days"><%= render 'week_days' %></div>

_week_days.html.erb

<%= search_form_for @q,remote: true do |f| %>
<%= f.label :datum_gteq %>
<%= f.date_field :datum_gteq%>
<%= f.label :datum_lteq %>
<%= f.date_field :datum_lteq %>
<%= f.submit %>
<% end %>
<table> 
<thead>
<tr>
  <th><%=sort_link @q, :datum %></th>
  <th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @week_days.each do |week_day| %>
  <tr>
    <td><%= l week_day.datum %></td>
    <td><%= link_to 'Show', week_day %></td>
    <td><%= link_to 'Edit', edit_week_day_path(week_day) %></td>

  </tr>
<% end %>
</tbody>
</table>
<%= paginate @week_days %>
<br>

<%= link_to 'New Week Day', new_week_day_path %>
31piy
  • 23,323
  • 6
  • 47
  • 67
T.Krmela
  • 33
  • 10

1 Answers1

1

some idea from me is you split the _week_days into partial and then render part of data using ajax, I added new div with name display-area as target for ajax to render and you should create javascripts to render it without reload the page with escape javascripts (j)

_week_days.html.erb

<%= render "search" %> 
<%= render "my_data" %> 

_search.html.erb

<%= search_form_for @q,remote: true do |f| %>
    <%= f.label :datum_gteq %>
    <%= f.date_field :datum_gteq%>
    <%= f.label :datum_lteq %>
    <%= f.date_field :datum_lteq %>
    <%= f.submit %>
<% end %>

_my_data.html.erb

<div class="display-area">
    <table> 
        <thead>
            <tr>
                  <th><%=sort_link @q, :datum %></th>
                  <th colspan="3"></th>
            </tr>
        </thead>
    <tbody>
    <% @week_days.each do |week_day| %>
      <tr>
        <td><%= l week_day.datum %></td>
        <td><%= link_to 'Show', week_day %></td>
        <td><%= link_to 'Edit', edit_week_day_path(week_day) %></td>

      </tr>
    <% end %>
    </tbody>
    </table>
    <%= paginate @week_days %>
    <br>
    <%= link_to 'New Week Day', new_week_day_path %>    
</div>

write _weekdays.js.erb inside app/assets/javascripts

$('.display-area').html("<%= j render(partial: 'my_data') %>") 
widjajayd
  • 6,090
  • 4
  • 30
  • 41