开发者

Links with data-remote="true" not processing as JSON

开发者 https://www.devze.com 2023-03-09 01:56 出处:网络
I am testing creating a link_to with :remote=> true to get the ajax remote link handling. <%= link_to \"Get Books\", \"/books\", :remote=> true %>

I am testing creating a link_to with :remote=> true to get the ajax remote link handling.

<%= link_to "Get Books", "/books", :remote=> true %>

The helper for rails3 resolves the code correctly in the view and I get as source:

<a href="/books" data-remote="true">Get Books</a>

And I have the default javascript included:

<%= javascript_include_tag :defaults %>

And the source head resolves as:

<script src="/javascripts/jquery.js?1302296105" type="text/javascript"></script>
<script开发者_JAVA百科 src="/javascripts/rails.js?1302139751" type="text/javascript"></script>
<script src="/javascripts/application.js?1305999509" type="text/javascript"></script>

And in the controller I have:

respond_to :html, :json

However when I click the link it loads as regular link, not ajax and on the server log:

Processing by BooksController#index as HTML

What am I missing?


Make sure that you have installed the jQuery UJS Plugin:

Add this to your gemfile:

gem 'jquery-rails', '>= 1.0.3'

And then run:

bundle install
rails g jquery:install


I had a similar issue: I expected a remote link to be processed as JS request but Rails somehow was processing it as HTML.

The problem is we don't specify a format and then Rails could choose any of the formats depending on the server configuration.

TL;DR; Specify the request type when using remote:

<%= link_to "Get Books", "/books", data: {remote: true, type: "script"} %>
0

精彩评论

暂无评论...
验证码 换一张
取 消