简体   繁体   English

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

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

I have the following code in ruby 1.9 rails 3.2. 我在ruby 1.9 rails 3.2中有以下代码。 I've followed a few different webpages to try to update a sub-field within a page. 我遵循了一些不同的网页来尝试更新页面中的子字段。 For some reason it's still redirecting to a new page, but it's not using the layout to render ... which is strange. 由于某种原因,它仍重定向到新页面,但未使用布局呈现...这很奇怪。

Routes: 路线:

match 'search' => 'content#search'

View: 视图:

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

Controller: 控制器:

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

I have an empty div within the html page with ID "ajaxstuffhere". 我在HTML页面中有一个ID为“ ajaxstuffhere”的空div。 When I click the submit button on the form, it loads the _search.html.erb in a new page instead of in the specified div. 当我单击表单上的提交按钮时,它将_search.html.erb加载到新页面中,而不是指定的div中。

Thanks for the help! 谢谢您的帮助!

Edit: Here is the post request from the server : 编辑:这是来自服务器的发布请求:

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)

Also, the HTML rendering of the form: 另外,表单的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>

Edit: 1 week later and still not functional 编辑:1周后,仍然无法正常工作

I have a new homepage.js.erb file with the following: 我有一个新的homepage.js.erb文件,其中包含以下内容:

("#ajaxstuffhere").hide();

Which allegedly is supposed to be returned and executed when data-remote="true" is set on the form. 据称应该在表单上设置data-remote="true"时返回并执行。 Note that I switched to the homepage controller until I can get an ajax response working..... 请注意,我切换到主页控制器,直到我能收到ajax响应为止。

Well ... that didn't work so I tried a coffee script alternative: 好吧...那没用,所以我尝试了一种咖啡脚本替代方法:

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

I dropped this right in the body of the html.erb file. 我将此权利放在了html.erb文件的主体中。 It should get executed when the page loads, I would think? 我认为应该在页面加载时执行它吗?

At any rate, if the data-remote="true" tag did the ajax request, the response should just add "response" to the ajaxstuffhere field ... except this is not the case. 无论如何,如果data-remote =“ true”标记执行了ajax请求,则响应应仅在ajaxstuffhere字段中添加“ response” ...除非不是这种情况。 The page still reloads and the AJAX response is probably lost 页面仍然重新加载,并且AJAX响应可能丢失了

Is your submit button in the right place? 您的提交按钮在正确的位置吗? This would be easier with a code snippet of the view with the form. 使用带有表单的视图的代码片段会更容易。

It turns out I have a line : 原来我有一行:

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

For Google Analytics, inside of the layout. 对于Google Analytics(分析),在布局内。 During a post request, the line forced the page to navigate to a new page. 在发布请求期间,该行强制页面导航到新页面。

I have no idea why this is the case, perhaps google has some kind of actionlistener that overrides the one that rails puts on for remote forms. 我不知道为什么会这样,也许谷歌有某种动作监听器,它覆盖了Rails为远程表单提供的动作监听器。

Any more information related to this is welcome 欢迎与此相关的更多信息

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

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