Kaminari index from both data-remote and html messing with links

僤鯓⒐⒋嵵緔 提交于 2020-01-06 19:37:09

问题


I have an index view for a model(nested ) that never gets called from the model, but is rendered from a couple different models. One view can render the index with either JS or html. With JS is it in a #related div in the show view and a data-remote link. Another option uses the same controller action to render it only has html.

All was fine until I added Kaminari paging. In the full page view, there was no #related div so paging didn't work. I broke out the guts into a partial and added a #related div and rendered the partial. From js I just rendered the partial from JS. That worked fine in the full page view, but in the show page it would render the partial, but the links didn't work, looks like it renders the entire show page. Clinking on another tab and then going back to the Progressions tab loads the partial and all works fine. It is just on the initial load of the show page that I can't get the page links to work.

I can load another tab that does not use paging first and solve my problem, but this was kind of the main information that you go to this page for.

Any ideas?

EDIT Request for code

The controller action method. The index method in this controller also sets @progressions

def progressions
  authorize! :read, Progression
  @stage = Stage.find(params[:id])
  @progressions = @stage.progressions_by_score.page(params[:page]).per(20)
  if params[:status] && !params[:status].blank? && @progressions
    @progressions =  @progressions.where(status: params[:status]).page(params[:page]).per(20)
  end
  respond_to do |format|
    format.js 
    format.html {render :template => "progressions/index"}
  end
end

The progressions.js.erb file in the stages view

$("#related").html("<%= escape_javascript(render('progressions/index'))%>");

The relations div in the show view. This is pretty much my scaffold template where I display or link to related information. The <div id="related"> is where any date-remote links will load the data. On initial load it loads the _index partial.

<div class="relations">
  <span class="navspan">
    <%= link_to 'Progressions: Status ->', progressions_stage_path(@stage), :'data-remote' => true, 
      :onclick => '$("#progression_status").val("")'  %> 
    <span class="huh">
      <%= hidden_field_tag :stage_id, params[:id]%>
      <%= select_tag :progression_status, options_for_select(Progression.statuses["all"], params[:status]), 
        :prompt => 'All', :class => 'f-left' %>
    </span>
    <%= link_to 'Assessors', assessors_stage_path(@stage), :'data-remote' => true %> 
    <%= link_to 'Events', events_stage_path(@stage), :'data-remote' => true %> 
    <%= link_to 'Select', select_stage_path(@stage) if can? :select_candidates, @stage %> 
    <%= link_to 'Ad Mgmt', edit_ad_stage_path(@stage) if can? :manage_ad, @stage %> 
  </span>
  <div class="f-clear"></div>
  <div id="related">
    <%= render "progressions/index"%>
  </div>
</div>

The index.html.haml file

#related
  = render "progressions/index"

The _index.html.haml file is just an index table listing the progressions but it does contain:

= paginate @progressions,  :remote => true

回答1:


Shortly after posting some code, I went back in my memory and used javascript to load the related div. I've tried to stay away from javascript, but in this case I added this to the end of the page after adding :id => "status_id" to the progressions link:

<script type="text/javascript" charset="utf-8">
  $(document).ready(function() {
    $("#status_link").trigger("click");
  })
</script>

While this fixes my problem, I still don't understand why the initial html response does not respond to the page links in the related div. I'll leave that as something else to learn. I'll put it in coffescript after I figure out how to have fire only on initial load of the show page.



来源:https://stackoverflow.com/questions/14187892/kaminari-index-from-both-data-remote-and-html-messing-with-links

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!