简体   繁体   English

带有URL参数的Rails UJS问题

[英]Rails UJS with URL params issue

I have a basic working rails app where I rely a lot on URL params for displaying or not logic. 我有一个基本的工作Rails应用程序,其中我非常依赖URL参数来显示或不显示逻辑。 I was able to ajaxify it using UJS and link_to remote => true but when I click these links, the shown URL isn't updated (which makes sense) and other partials can't read the URL params 我可以使用UJS和link_to remote => true将其关闭,但是当我单击这些链接时,显示的URL未更新(这很有意义),其他部分无法读取URL参数

For example, in index.html.erb (before UJS so you can see how it works), 例如,在index.html.erb (在UJS之前,因此您可以查看其工作原理),

<div class="container-fluid">
  <% if params[:category] %><div class="row-fluid span6">
    <div class="span6">
      <%= render :partial => "buglist" %>
    </div>
  <% end %>

  <% if params[:id] %>
    <div class="span6">
      <%= render :partial => "bugdetails" %>
    </div>
  <% end %>

  <% unless params[:category] or params[:id]%>
    <%= render :partial => "landingpage" %>
  <% end %>
</div>
  1. My navbar has link_to for multiple :category, like this 我的导航栏具有多个:category的link_to,像这样

    "top10", :action => "index", :category => catname) %> “ top10”,:action =>“ index”,:category => catname)%>
  2. If there is a params[:category] in the URL, I display a list on the left and when I click an ID in this list, it'll display the right DIV with 如果网址中有params [:category],则在左侧显示一个列表,当单击此列表中的ID时,它将显示右侧的DIV

  3. If there is no params, it'll render what I call the landing page (some kind of homepage) 如果没有参数,它将呈现我所谓的登录页面(某种首页)

This works good now but if I add remote => true to my navbar links, it'll work for the left div but the #3 (landing page) will always display because for some reason it won't see the URL params. 现在效果很好,但是如果我在我的导航栏链接中添加remote => true,它将对左div起作用,但由于某些原因,它始终会显示#3(着陆页),因为它看不到URL参数。 Also, since the shown URL is always the same, all my link_to_unless(params...) won't work. 另外,由于显示的URL始终相同,所以我所有的link_to_unless(params ...)都无法正常工作。

With USJ: 使用USJ:

index.html.erb index.html.erb

<div class="row-fluid span6" id="buglist"></div>
<div class="row-fluid span6" id="bugdetails"></div>
<div class="row-fluid id="landingpage"></div>

index.js.erb index.js.erb

<% if params[:category] %>
  $("#buglist").html("<%= escape_javascript(render(:partial => 'buglist')) %>");
<% end %>

<% if params[:id] %>
  $("#bugdetails").html("<%= escape_javascript(render(:partial => 'bugdetails')) %>");
<% end %>

<% unless params[:category] or params[:id]%>
  $("#landingpage").html("<%= escape_javascript(render(:partial => 'landingpage')) %>");
<% end %>

If you are switching functionality to ajax and not doing page reloads, you may wish to consider putting those url params as session variables instead. 如果您要将功能切换到ajax而不进行页面重新加载,则不妨考虑将这些url参数用作会话变量。 That way you can update and show them as desired. 这样,您可以根据需要更新并显示它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM