繁体   English   中英

Ajax在rails中,仍然重定向到新页面

[英]Ajax in rails, still redirecting to new page

我在ruby 1.9 rails 3.2中有以下代码。 我遵循了一些不同的网页来尝试更新页面中的子字段。 由于某种原因,它仍重定向到新页面,但未使用布局呈现...这很奇怪。

路线:

match 'search' => 'content#search'

视图:

<%= form_tag({:action => 'search'},:id => 'searchForm',:remote => true, :update => "ajaxstuffhere", :position => :bottom) do %>
 <p>
                     <label id="parkname">Park Name:</label><br/> 
                     <%= text_field :search, :parkname, :size => 25 %>
                  </p>
                  <p>
                     <label id="stateprovince">State / Province:</label><br/>
                     <% tstate = State.new %>
                     <% tstate.name = "Select a State / Province" %>
                     <% @states = [] %>
                     <% @states << tstate %>
                     <% states = State.all.sort{|x,y| [x.country_id, x.name] <=> [y.country_id, y.name] } %>
                     <% states.each do |state| %>
                        <% @states << state %>
                     <% end %>
                     <%= puts "statelist is : " + @states.inspect.to_s %> 
                     <%= collection_select(:search, :state, @states, :id, :name) %>
                  </p>
                  <p>
                     <label id="zipcode">Zip Code:</label><br/> 
                     <%= text_field :search, :zipcode, :size => 5 %>

                  </p>
                  <p>
                     <label id="distanceSearchLabel">Max Distance:</label><br/>

                    <select id="distancePick" name="search[distance]">
                      <option value="">Select Distance</option>
                      <option value="10">10km</option>
                      <option value="50">50km</option>
                      <option value="100">100km</option>
                      <option value="250">250km</option>
                      <option value="500">500km</option>
                      <option value="1000">1000km</option>
                    </select>
                  </p>
                  </p>
                  <p>
                    <%= submit_tag 'Search' %>
                  </p>

               <div style="clear:both;">

                </div>


            <h2>Narrow your Search</h2>


            <a href="JavaScript:amenitiesPopup()">By Amenities</a>
            <br />

            <p>
               <label id="parkname">Price:</label><br/> 
               <%= text_field :search, :pricelow, :size => 5 %>
               <%= text_field :search, :pricehigh, :size => 5 %>
            </p>
            <p>
               <label id="parkname">Age:</label><br/> 
               <%= text_field :search, :age, :size => 5 %>
            </p>
            <p>

            <%= check_box :search,:pets_allowed %><label id="petsallowedcb">Allows Pets</label><br/> 
            </p>

            <%= check_box :search,:big_rigs %><label id="bigrigcb">Allows Big Rigs</label><br/> 

            <p>
            <%= submit_tag 'Search' %>
          </p>
          <% end %>

控制器:

def search
    ...
   render :partial => 'search'
end #seach def

我在HTML页面中有一个ID为“ ajaxstuffhere”的空div。 当我单击表单上的提交按钮时,它将_search.html.erb加载到新页面中,而不是指定的div中。

谢谢您的帮助!

编辑:这是来自服务器的发布请求:

Started POST "/search" for 70.28.21.25 at 2013-09-25 13:22:54 +0000
Processing by ContentController#search as HTML
  Parameters: {"utf8"=>"â", "search"=>{"parkname"=>"as", "state"=>"", "zipcode"=>"", "distance"=>"", "pricelow"=>"", "pricehigh"=>"", "age"=>"", "pets_allowed"=>"0", "big_rigs"=>"0"}}
Rendered content/_search.html.erb (424.3ms)
Completed 200 OK in 637ms (Views: 105.6ms | ActiveRecord: 478.6ms)

另外,表单的HTML呈现:

<form accept-charset="UTF-8" action="/search" data-remote="true" id="searchForm" method="post" position="bottom" update="ajaxstuffhere"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /></div>

                  <p>
                     <label id="parkname">Park Name:</label><br/> 
                     <input id="search_parkname" name="search[parkname]" size="25" type="text" />
                  </p>
...
<input name="commit" type="submit" value="Search" />
</form>

编辑:1周后,仍然无法正常工作

我有一个新的homepage.js.erb文件,其中包含以下内容:

("#ajaxstuffhere").hide();

据称应该在表单上设置data-remote="true"时返回并执行。 请注意,我切换到主页控制器,直到我能收到ajax响应为止。

好吧...那没用,所以我尝试了一种咖啡脚本替代方法:

<script>
$(document).ready ->
  $("#ajaxstuffhere").on("ajax:success", (e, data, status, xhr) ->
    $("#ajaxstuffhere").append "<p>RESPONSE</p>" //xhr.responseText
  ).bind "ajax:error", (e, xhr, status, error) ->
    $("#ajaxstuffhere").append "<p>ERROR</p>"
</script>

我将此权利放在了html.erb文件的主体中。 我认为应该在页面加载时执行它吗?

无论如何,如果data-remote =“ true”标记执行了ajax请求,则响应应仅在ajaxstuffhere字段中添加“ response” ...除非不是这种情况。 页面仍然重新加载,并且AJAX响应可能丢失了

您的提交按钮在正确的位置吗? 使用带有表单的视图的代码片段会更容易。

原来我有一行:

<%= render :partial => '/inc/analytics' %>

对于Google Analytics(分析),在布局内。 在发布请求期间,该行强制页面导航到新页面。

我不知道为什么会这样,也许谷歌有某种动作监听器,它覆盖了Rails为远程表单提供的动作监听器。

欢迎与此相关的更多信息

暂无
暂无

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

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