Reputation: 57
I am trying to load a post's body (model's attribute) from ajax so the body can appear in a div when a button get's clicked. An option to possibly do this is by having a show page for each post that just gets inserted inside a div and has all of the information inside of it, but the show page already has different content than the one that has to be inserted inside the div. Would I have to load the attribute using a js.erb?
Also, already having the post's body on the page before hand is not an option because the bodies are very long and there could be multiple posts.
Upvotes: 0
Views: 198
Reputation: 433
There are many ways to load model attribute but I assume you are asking which is the most simple. Using js.erb makes it really simple indeed.
put the codes from show page that will be reused to partial. For example _object.html.erb
make your ajax call with datatype :script $.ajax({
type: "GET",
url: $(this).attr('href'),
data: {
object_id: object_id
},
dataType: "script",
success: function () {
}
});
On the receiving side of ajax url, set the object and create js.erb file inside the view folders
render partial with escape javascript
<%= escape_javascript(render(@obejct))%>
Upvotes: 1