Rails (5.2.2) AJAX call for partial not working
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
Short and sweet, I have followed this guide exactly on a fresh rails deployment (5.2.2), using rails g scaffold thing to generate the app.
However, after making the partial _show.html.erb under the paragraph Targeted Changes in the guide, clicking the "Change below" link_to does not render the partial when testing. I get no error message, and Rails tells me that everything is rendered OK, but nothing happens on the site when I click the link.
What am I missing?
Code:
controllers/things_controller
class ThingsController < ApplicationController
def index
@things = Thing.all
end
def show
end
end
config/routes.rb
Rails.application.routes.draw do
root 'things#index'
resources :things
end
things/index.html.erb
<%# /app/views/things/index.html.erb %>
<%= link_to 'Change Below', thing_path(42), remote: true %>
<div id="target-for-change">
Now you see "ME"!
</div>
things/show.js.erb
<%# /app/views/things/show.js.erb %>
$("#target-for-change").html("<%= j render(partial: 'show') %>");
things/_show.html.erb
<%# /app/views/things/_show.html.erb %>
Now you don't!
Browser Console output:
Uncaught ReferenceError: $ is not defined
at <anonymous>:1:1
at processResponse (rails-ujs.self-d109d8c5c0194c8ad60b8838b2661c5596b5c955987f7cd4045eb2fb90ca5343.js?body=1:268)
at rails-ujs.self-d109d8c5c0194c8ad60b8838b2661c5596b5c955987f7cd4045eb2fb90ca5343.js?body=1:196
at XMLHttpRequest.xhr.onreadystatechange (rails-ujs.self-d109d8c5c0194c8ad60b8838b2661c5596b5c955987f7cd4045eb2fb90ca5343.js?body=1:251)
ruby-on-rails ajax ruby-on-rails-5 partial-views
add a comment |
Short and sweet, I have followed this guide exactly on a fresh rails deployment (5.2.2), using rails g scaffold thing to generate the app.
However, after making the partial _show.html.erb under the paragraph Targeted Changes in the guide, clicking the "Change below" link_to does not render the partial when testing. I get no error message, and Rails tells me that everything is rendered OK, but nothing happens on the site when I click the link.
What am I missing?
Code:
controllers/things_controller
class ThingsController < ApplicationController
def index
@things = Thing.all
end
def show
end
end
config/routes.rb
Rails.application.routes.draw do
root 'things#index'
resources :things
end
things/index.html.erb
<%# /app/views/things/index.html.erb %>
<%= link_to 'Change Below', thing_path(42), remote: true %>
<div id="target-for-change">
Now you see "ME"!
</div>
things/show.js.erb
<%# /app/views/things/show.js.erb %>
$("#target-for-change").html("<%= j render(partial: 'show') %>");
things/_show.html.erb
<%# /app/views/things/_show.html.erb %>
Now you don't!
Browser Console output:
Uncaught ReferenceError: $ is not defined
at <anonymous>:1:1
at processResponse (rails-ujs.self-d109d8c5c0194c8ad60b8838b2661c5596b5c955987f7cd4045eb2fb90ca5343.js?body=1:268)
at rails-ujs.self-d109d8c5c0194c8ad60b8838b2661c5596b5c955987f7cd4045eb2fb90ca5343.js?body=1:196
at XMLHttpRequest.xhr.onreadystatechange (rails-ujs.self-d109d8c5c0194c8ad60b8838b2661c5596b5c955987f7cd4045eb2fb90ca5343.js?body=1:251)
ruby-on-rails ajax ruby-on-rails-5 partial-views
2
What's the response browser console?
– Talha Junaid
Jan 3 at 10:29
are you usingturbolinks? If yes then try after removingturbolinks
– G.B
Jan 3 at 11:35
@TalhaJunaid, I've added the Browser Console output.
– tmo
Jan 3 at 14:22
@G.B, removing turbolinks does not solve it, same behaviour.
– tmo
Jan 3 at 14:22
@tmo I have added the answer, please view this
– Talha Junaid
Jan 3 at 14:30
add a comment |
Short and sweet, I have followed this guide exactly on a fresh rails deployment (5.2.2), using rails g scaffold thing to generate the app.
However, after making the partial _show.html.erb under the paragraph Targeted Changes in the guide, clicking the "Change below" link_to does not render the partial when testing. I get no error message, and Rails tells me that everything is rendered OK, but nothing happens on the site when I click the link.
What am I missing?
Code:
controllers/things_controller
class ThingsController < ApplicationController
def index
@things = Thing.all
end
def show
end
end
config/routes.rb
Rails.application.routes.draw do
root 'things#index'
resources :things
end
things/index.html.erb
<%# /app/views/things/index.html.erb %>
<%= link_to 'Change Below', thing_path(42), remote: true %>
<div id="target-for-change">
Now you see "ME"!
</div>
things/show.js.erb
<%# /app/views/things/show.js.erb %>
$("#target-for-change").html("<%= j render(partial: 'show') %>");
things/_show.html.erb
<%# /app/views/things/_show.html.erb %>
Now you don't!
Browser Console output:
Uncaught ReferenceError: $ is not defined
at <anonymous>:1:1
at processResponse (rails-ujs.self-d109d8c5c0194c8ad60b8838b2661c5596b5c955987f7cd4045eb2fb90ca5343.js?body=1:268)
at rails-ujs.self-d109d8c5c0194c8ad60b8838b2661c5596b5c955987f7cd4045eb2fb90ca5343.js?body=1:196
at XMLHttpRequest.xhr.onreadystatechange (rails-ujs.self-d109d8c5c0194c8ad60b8838b2661c5596b5c955987f7cd4045eb2fb90ca5343.js?body=1:251)
ruby-on-rails ajax ruby-on-rails-5 partial-views
Short and sweet, I have followed this guide exactly on a fresh rails deployment (5.2.2), using rails g scaffold thing to generate the app.
However, after making the partial _show.html.erb under the paragraph Targeted Changes in the guide, clicking the "Change below" link_to does not render the partial when testing. I get no error message, and Rails tells me that everything is rendered OK, but nothing happens on the site when I click the link.
What am I missing?
Code:
controllers/things_controller
class ThingsController < ApplicationController
def index
@things = Thing.all
end
def show
end
end
config/routes.rb
Rails.application.routes.draw do
root 'things#index'
resources :things
end
things/index.html.erb
<%# /app/views/things/index.html.erb %>
<%= link_to 'Change Below', thing_path(42), remote: true %>
<div id="target-for-change">
Now you see "ME"!
</div>
things/show.js.erb
<%# /app/views/things/show.js.erb %>
$("#target-for-change").html("<%= j render(partial: 'show') %>");
things/_show.html.erb
<%# /app/views/things/_show.html.erb %>
Now you don't!
Browser Console output:
Uncaught ReferenceError: $ is not defined
at <anonymous>:1:1
at processResponse (rails-ujs.self-d109d8c5c0194c8ad60b8838b2661c5596b5c955987f7cd4045eb2fb90ca5343.js?body=1:268)
at rails-ujs.self-d109d8c5c0194c8ad60b8838b2661c5596b5c955987f7cd4045eb2fb90ca5343.js?body=1:196
at XMLHttpRequest.xhr.onreadystatechange (rails-ujs.self-d109d8c5c0194c8ad60b8838b2661c5596b5c955987f7cd4045eb2fb90ca5343.js?body=1:251)
ruby-on-rails ajax ruby-on-rails-5 partial-views
ruby-on-rails ajax ruby-on-rails-5 partial-views
edited Jan 3 at 14:17
tmo
asked Jan 3 at 9:47
tmotmo
453622
453622
2
What's the response browser console?
– Talha Junaid
Jan 3 at 10:29
are you usingturbolinks? If yes then try after removingturbolinks
– G.B
Jan 3 at 11:35
@TalhaJunaid, I've added the Browser Console output.
– tmo
Jan 3 at 14:22
@G.B, removing turbolinks does not solve it, same behaviour.
– tmo
Jan 3 at 14:22
@tmo I have added the answer, please view this
– Talha Junaid
Jan 3 at 14:30
add a comment |
2
What's the response browser console?
– Talha Junaid
Jan 3 at 10:29
are you usingturbolinks? If yes then try after removingturbolinks
– G.B
Jan 3 at 11:35
@TalhaJunaid, I've added the Browser Console output.
– tmo
Jan 3 at 14:22
@G.B, removing turbolinks does not solve it, same behaviour.
– tmo
Jan 3 at 14:22
@tmo I have added the answer, please view this
– Talha Junaid
Jan 3 at 14:30
2
2
What's the response browser console?
– Talha Junaid
Jan 3 at 10:29
What's the response browser console?
– Talha Junaid
Jan 3 at 10:29
are you using
turbolinks? If yes then try after removing turbolinks– G.B
Jan 3 at 11:35
are you using
turbolinks? If yes then try after removing turbolinks– G.B
Jan 3 at 11:35
@TalhaJunaid, I've added the Browser Console output.
– tmo
Jan 3 at 14:22
@TalhaJunaid, I've added the Browser Console output.
– tmo
Jan 3 at 14:22
@G.B, removing turbolinks does not solve it, same behaviour.
– tmo
Jan 3 at 14:22
@G.B, removing turbolinks does not solve it, same behaviour.
– tmo
Jan 3 at 14:22
@tmo I have added the answer, please view this
– Talha Junaid
Jan 3 at 14:30
@tmo I have added the answer, please view this
– Talha Junaid
Jan 3 at 14:30
add a comment |
2 Answers
2
active
oldest
votes
As you can see on your console output, '$' is not defined. '$' is the common alias for jQuery so when you do:
$("#target-for-change").html("<%= j render(partial: 'show') %>");
you are telling jQuery to find a certain identifier. The thing is that you did not include the library in your project. A way to do it is by adding this line in your gemfile:
gem 'jquery-rails'
and then this in your assets/javascripts/application.js
//= require jquery3
Hi Rodrigo, and welcome so SO. I just read that jquery was discontinued in 5.1, so that must be it.
– tmo
Jan 4 at 8:12
add a comment |
You need to add the jquery in your project. Add this line to application.html.erb
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
Or you can use the gem jquery-rails
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54019765%2frails-5-2-2-ajax-call-for-partial-not-working%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
As you can see on your console output, '$' is not defined. '$' is the common alias for jQuery so when you do:
$("#target-for-change").html("<%= j render(partial: 'show') %>");
you are telling jQuery to find a certain identifier. The thing is that you did not include the library in your project. A way to do it is by adding this line in your gemfile:
gem 'jquery-rails'
and then this in your assets/javascripts/application.js
//= require jquery3
Hi Rodrigo, and welcome so SO. I just read that jquery was discontinued in 5.1, so that must be it.
– tmo
Jan 4 at 8:12
add a comment |
As you can see on your console output, '$' is not defined. '$' is the common alias for jQuery so when you do:
$("#target-for-change").html("<%= j render(partial: 'show') %>");
you are telling jQuery to find a certain identifier. The thing is that you did not include the library in your project. A way to do it is by adding this line in your gemfile:
gem 'jquery-rails'
and then this in your assets/javascripts/application.js
//= require jquery3
Hi Rodrigo, and welcome so SO. I just read that jquery was discontinued in 5.1, so that must be it.
– tmo
Jan 4 at 8:12
add a comment |
As you can see on your console output, '$' is not defined. '$' is the common alias for jQuery so when you do:
$("#target-for-change").html("<%= j render(partial: 'show') %>");
you are telling jQuery to find a certain identifier. The thing is that you did not include the library in your project. A way to do it is by adding this line in your gemfile:
gem 'jquery-rails'
and then this in your assets/javascripts/application.js
//= require jquery3
As you can see on your console output, '$' is not defined. '$' is the common alias for jQuery so when you do:
$("#target-for-change").html("<%= j render(partial: 'show') %>");
you are telling jQuery to find a certain identifier. The thing is that you did not include the library in your project. A way to do it is by adding this line in your gemfile:
gem 'jquery-rails'
and then this in your assets/javascripts/application.js
//= require jquery3
answered Jan 3 at 14:48
Rodrigo EcheconeaRodrigo Echeconea
963
963
Hi Rodrigo, and welcome so SO. I just read that jquery was discontinued in 5.1, so that must be it.
– tmo
Jan 4 at 8:12
add a comment |
Hi Rodrigo, and welcome so SO. I just read that jquery was discontinued in 5.1, so that must be it.
– tmo
Jan 4 at 8:12
Hi Rodrigo, and welcome so SO. I just read that jquery was discontinued in 5.1, so that must be it.
– tmo
Jan 4 at 8:12
Hi Rodrigo, and welcome so SO. I just read that jquery was discontinued in 5.1, so that must be it.
– tmo
Jan 4 at 8:12
add a comment |
You need to add the jquery in your project. Add this line to application.html.erb
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
Or you can use the gem jquery-rails
add a comment |
You need to add the jquery in your project. Add this line to application.html.erb
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
Or you can use the gem jquery-rails
add a comment |
You need to add the jquery in your project. Add this line to application.html.erb
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
Or you can use the gem jquery-rails
You need to add the jquery in your project. Add this line to application.html.erb
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
Or you can use the gem jquery-rails
answered Jan 3 at 14:27
Talha JunaidTalha Junaid
230210
230210
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54019765%2frails-5-2-2-ajax-call-for-partial-not-working%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown

2
What's the response browser console?
– Talha Junaid
Jan 3 at 10:29
are you using
turbolinks? If yes then try after removingturbolinks– G.B
Jan 3 at 11:35
@TalhaJunaid, I've added the Browser Console output.
– tmo
Jan 3 at 14:22
@G.B, removing turbolinks does not solve it, same behaviour.
– tmo
Jan 3 at 14:22
@tmo I have added the answer, please view this
– Talha Junaid
Jan 3 at 14:30