Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 3 - How do I dynamically create an html element that is associated with a param[:id]?
    primarykey
    data
    text
    <p>I'm attempting to implement a Rails 3 application that needs to use the html5 drawing canvas.</p> <p>So far I have a Sketch scaffold and I've modified the <strong>index</strong> and <strong>show</strong> views to be side-by-side (I typed the hidden <strong>div</strong> in there just to show that I plan to host the canvas in a div):</p> <p><img src="https://i.stack.imgur.com/D23if.png" alt="enter image description here"></p> <p>Currently in the <strong>application.js</strong> file I have a script that accesses the show action through an Ajax request and places it right next to the Sketches list. Here is the code for that:</p> <pre><code>// Show individual sketch $(function(){ var $sketch_div = $('#show_sketch_div'); // return html instead of js $('a[data-remote]').live('ajax:beforeSend', function(e, xhr, settings){ xhr.setRequestHeader('accept', '*/*;q=0.5, text/html, ' + settings.accepts.html); }); // Handle sketch name links with the data-remote attribute $('a[data-remote]').live('ajax:success', function(xhr, data, status){ $sketch_div .html(data) .show(); }); }); </code></pre> <p>Also, each of the links in the Sketches list has a class associated with it (in order to grab it with JQuery):</p> <pre><code>&lt;td&gt;&lt;%= link_to 'Show', sketch, :remote =&gt; true, :class =&gt; 'show_sketch_button' %&gt;&lt;/td&gt; </code></pre> <p>I have the Sketch controller set up to render JSON:</p> <blockquote> <p>def show @sketch = Sketch.find(params[:id])</p> <pre><code>respond_to do |format| format.html # show.html.erb format.xml { render :xml =&gt; @sketch } format.json { render :json =&gt; @sketch } end </code></pre> <p>end</p> </blockquote> <p>This part is where I'm really stuck. </p> <p>I plan on using JQuery to insert a Canvas tag whenever a Sketch's <strong>show</strong> button is clicked. However, I have no idea how to get the <strong>id</strong> paramater of the associated sketch with Javascript and then use that to generate a Canvas element that is only associated with that particular <strong>id</strong>.</p> <p>I've searched for JSON tutorials but I can't find anything that seems to address what I'm trying to do. I'm also very new to JSON - so I could just be missing it. </p> <p>Does anyone have any ideas or an example that you can point me to?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload