【问题标题】:rails ransack remote true with paginationrails ransack remote true 带分页
【发布时间】:2017-07-13 12:42:10
【问题描述】:

我有一个WeekDay 的控制器。然后我尝试在我的项目中添加 RanSack。它工作得很好,除非我尝试启用remote true,它只显示搜索表单。正在进行 AJAX 调用,但没有任何显示。

控制器代码

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 %>

【问题讨论】:

  • 改进的语法和代码格式。删除了 404 错误的要点链接。

标签: ruby-on-rails ruby ajax pagination


【解决方案1】:

我的一些想法是您将 _week_days 拆分为部分数据,然后使用 ajax 渲染部分数据,我添加了名称为 display-area 的新 div 作为 ajax 渲染的目标,您应该创建 javascripts 来渲染它而无需重新加载页面使用转义 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>

在 app/assets/javascripts 中写入 _weekdays.js.erb

$('.display-area').html("<%= j render(partial: 'my_data') %>") 

【讨论】:

  • ActionView::Template::Error (undefined method `render' for #:0x007f0a31e26e30>) 用于该javascript行
  • 已将我的 js.erb 移至 views 文件夹,请参阅stackoverflow.com/questions/8370399/…
  • ActionView::Template::Error (#:0x007f28a2341c10> 的未定义方法 `render'):我将它移动到与 html 模板 rails 5.1 相同的文件夹中
  • 其实我在 ransack 中选择的任何东西都会被发送回服务器,但它根本不会呈现
  • 我在控制台中看到它是使用 ajax 发送的,但它不会在应用程序中呈现
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-26
  • 2014-08-14
  • 1970-01-01
  • 1970-01-01
  • 2017-06-11
相关资源
最近更新 更多