How to pass the attributes in ajax which can be accessed through params in the controller in ruby on rails?











up vote
0
down vote

favorite












My index.html.erb file looks like:
index.html.erb



...
<div class="panel-heading">
<h4 class="panel-title">
<li class="list-group-item">
Post --> <%= post.message %>
<%= link_to "Like", 'javascript:void(0)', class: "btn btn-default like-right-position like-btn"%>
<span class="like-right-position like-text">Likdfe</span>
</li>
<a class="btn btn-info pull-right" data-parent="#accordion1" data-toggle="collapse" href="#collapse-post-<%= post.id %>"> Comments</a>
</h4>
</div>
...


And my application.js file is:



$('.like-btn').click(function(){
$.ajax({
type: "POST",
url: "like_user_post_path",
data: "",
success: function(result){
$(".like-text").html(result);
}
});
});


I do not understand the concept of data attributes in html and how to pass it through ajax. Thanks in advance!!!










share|improve this question







New contributor




Pujan Soni is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Why do not you use data field inside ajax block ?
    – ray
    22 hours ago















up vote
0
down vote

favorite












My index.html.erb file looks like:
index.html.erb



...
<div class="panel-heading">
<h4 class="panel-title">
<li class="list-group-item">
Post --> <%= post.message %>
<%= link_to "Like", 'javascript:void(0)', class: "btn btn-default like-right-position like-btn"%>
<span class="like-right-position like-text">Likdfe</span>
</li>
<a class="btn btn-info pull-right" data-parent="#accordion1" data-toggle="collapse" href="#collapse-post-<%= post.id %>"> Comments</a>
</h4>
</div>
...


And my application.js file is:



$('.like-btn').click(function(){
$.ajax({
type: "POST",
url: "like_user_post_path",
data: "",
success: function(result){
$(".like-text").html(result);
}
});
});


I do not understand the concept of data attributes in html and how to pass it through ajax. Thanks in advance!!!










share|improve this question







New contributor




Pujan Soni is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Why do not you use data field inside ajax block ?
    – ray
    22 hours ago













up vote
0
down vote

favorite









up vote
0
down vote

favorite











My index.html.erb file looks like:
index.html.erb



...
<div class="panel-heading">
<h4 class="panel-title">
<li class="list-group-item">
Post --> <%= post.message %>
<%= link_to "Like", 'javascript:void(0)', class: "btn btn-default like-right-position like-btn"%>
<span class="like-right-position like-text">Likdfe</span>
</li>
<a class="btn btn-info pull-right" data-parent="#accordion1" data-toggle="collapse" href="#collapse-post-<%= post.id %>"> Comments</a>
</h4>
</div>
...


And my application.js file is:



$('.like-btn').click(function(){
$.ajax({
type: "POST",
url: "like_user_post_path",
data: "",
success: function(result){
$(".like-text").html(result);
}
});
});


I do not understand the concept of data attributes in html and how to pass it through ajax. Thanks in advance!!!










share|improve this question







New contributor




Pujan Soni is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











My index.html.erb file looks like:
index.html.erb



...
<div class="panel-heading">
<h4 class="panel-title">
<li class="list-group-item">
Post --> <%= post.message %>
<%= link_to "Like", 'javascript:void(0)', class: "btn btn-default like-right-position like-btn"%>
<span class="like-right-position like-text">Likdfe</span>
</li>
<a class="btn btn-info pull-right" data-parent="#accordion1" data-toggle="collapse" href="#collapse-post-<%= post.id %>"> Comments</a>
</h4>
</div>
...


And my application.js file is:



$('.like-btn').click(function(){
$.ajax({
type: "POST",
url: "like_user_post_path",
data: "",
success: function(result){
$(".like-text").html(result);
}
});
});


I do not understand the concept of data attributes in html and how to pass it through ajax. Thanks in advance!!!







jquery ruby-on-rails ruby ajax






share|improve this question







New contributor




Pujan Soni is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Pujan Soni is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Pujan Soni is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 22 hours ago









Pujan Soni

83




83




New contributor




Pujan Soni is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Pujan Soni is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Pujan Soni is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • Why do not you use data field inside ajax block ?
    – ray
    22 hours ago


















  • Why do not you use data field inside ajax block ?
    – ray
    22 hours ago
















Why do not you use data field inside ajax block ?
– ray
22 hours ago




Why do not you use data field inside ajax block ?
– ray
22 hours ago












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










As per the description mentioned in the post, the data attrbutes are the ones which are sent as body for the following POST request via ajax.



$('.like-btn').click(function(){
$.ajax({
type: "POST",
url: "like_user_post_path",
data: {"field1": "value1", "field2": "value2"},
success: function(result){
$(".like-text").html(result);
}
});
});


Above is an example where the field1 and field2 specify the keys from which you will be parsing over the controller to fetch the corresponding values.






share|improve this answer





















  • How can we set the same data attributes in the link_to tag?
    – Pujan Soni
    21 hours ago










  • link_to "abc", abc_path(@abc.id, param1: 'value1', param2: 'value2'), method: :post
    – Rohan
    21 hours ago










  • Thanks a lot!!! @Rohan
    – Pujan Soni
    20 hours ago












  • It would be great if you could mark it as a correct, if it was helpful so that others can benefit from the same.
    – Rohan
    19 hours ago













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',
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
});


}
});






Pujan Soni is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372105%2fhow-to-pass-the-attributes-in-ajax-which-can-be-accessed-through-params-in-the-c%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










As per the description mentioned in the post, the data attrbutes are the ones which are sent as body for the following POST request via ajax.



$('.like-btn').click(function(){
$.ajax({
type: "POST",
url: "like_user_post_path",
data: {"field1": "value1", "field2": "value2"},
success: function(result){
$(".like-text").html(result);
}
});
});


Above is an example where the field1 and field2 specify the keys from which you will be parsing over the controller to fetch the corresponding values.






share|improve this answer





















  • How can we set the same data attributes in the link_to tag?
    – Pujan Soni
    21 hours ago










  • link_to "abc", abc_path(@abc.id, param1: 'value1', param2: 'value2'), method: :post
    – Rohan
    21 hours ago










  • Thanks a lot!!! @Rohan
    – Pujan Soni
    20 hours ago












  • It would be great if you could mark it as a correct, if it was helpful so that others can benefit from the same.
    – Rohan
    19 hours ago

















up vote
0
down vote



accepted










As per the description mentioned in the post, the data attrbutes are the ones which are sent as body for the following POST request via ajax.



$('.like-btn').click(function(){
$.ajax({
type: "POST",
url: "like_user_post_path",
data: {"field1": "value1", "field2": "value2"},
success: function(result){
$(".like-text").html(result);
}
});
});


Above is an example where the field1 and field2 specify the keys from which you will be parsing over the controller to fetch the corresponding values.






share|improve this answer





















  • How can we set the same data attributes in the link_to tag?
    – Pujan Soni
    21 hours ago










  • link_to "abc", abc_path(@abc.id, param1: 'value1', param2: 'value2'), method: :post
    – Rohan
    21 hours ago










  • Thanks a lot!!! @Rohan
    – Pujan Soni
    20 hours ago












  • It would be great if you could mark it as a correct, if it was helpful so that others can benefit from the same.
    – Rohan
    19 hours ago















up vote
0
down vote



accepted







up vote
0
down vote



accepted






As per the description mentioned in the post, the data attrbutes are the ones which are sent as body for the following POST request via ajax.



$('.like-btn').click(function(){
$.ajax({
type: "POST",
url: "like_user_post_path",
data: {"field1": "value1", "field2": "value2"},
success: function(result){
$(".like-text").html(result);
}
});
});


Above is an example where the field1 and field2 specify the keys from which you will be parsing over the controller to fetch the corresponding values.






share|improve this answer












As per the description mentioned in the post, the data attrbutes are the ones which are sent as body for the following POST request via ajax.



$('.like-btn').click(function(){
$.ajax({
type: "POST",
url: "like_user_post_path",
data: {"field1": "value1", "field2": "value2"},
success: function(result){
$(".like-text").html(result);
}
});
});


Above is an example where the field1 and field2 specify the keys from which you will be parsing over the controller to fetch the corresponding values.







share|improve this answer












share|improve this answer



share|improve this answer










answered 22 hours ago









Rohan

9791311




9791311












  • How can we set the same data attributes in the link_to tag?
    – Pujan Soni
    21 hours ago










  • link_to "abc", abc_path(@abc.id, param1: 'value1', param2: 'value2'), method: :post
    – Rohan
    21 hours ago










  • Thanks a lot!!! @Rohan
    – Pujan Soni
    20 hours ago












  • It would be great if you could mark it as a correct, if it was helpful so that others can benefit from the same.
    – Rohan
    19 hours ago




















  • How can we set the same data attributes in the link_to tag?
    – Pujan Soni
    21 hours ago










  • link_to "abc", abc_path(@abc.id, param1: 'value1', param2: 'value2'), method: :post
    – Rohan
    21 hours ago










  • Thanks a lot!!! @Rohan
    – Pujan Soni
    20 hours ago












  • It would be great if you could mark it as a correct, if it was helpful so that others can benefit from the same.
    – Rohan
    19 hours ago


















How can we set the same data attributes in the link_to tag?
– Pujan Soni
21 hours ago




How can we set the same data attributes in the link_to tag?
– Pujan Soni
21 hours ago












link_to "abc", abc_path(@abc.id, param1: 'value1', param2: 'value2'), method: :post
– Rohan
21 hours ago




link_to "abc", abc_path(@abc.id, param1: 'value1', param2: 'value2'), method: :post
– Rohan
21 hours ago












Thanks a lot!!! @Rohan
– Pujan Soni
20 hours ago






Thanks a lot!!! @Rohan
– Pujan Soni
20 hours ago














It would be great if you could mark it as a correct, if it was helpful so that others can benefit from the same.
– Rohan
19 hours ago






It would be great if you could mark it as a correct, if it was helpful so that others can benefit from the same.
– Rohan
19 hours ago












Pujan Soni is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















Pujan Soni is a new contributor. Be nice, and check out our Code of Conduct.













Pujan Soni is a new contributor. Be nice, and check out our Code of Conduct.












Pujan Soni is a new contributor. Be nice, and check out our Code of Conduct.















 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372105%2fhow-to-pass-the-attributes-in-ajax-which-can-be-accessed-through-params-in-the-c%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

SQL update select statement

'app-layout' is not a known element: how to share Component with different Modules