Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think you are almost there. </p> <p>Firstly - yes, changing the css with javascript on runtime will immediately change the appearance of dom elements.</p> <p>On the client side - you want to indicate to the user which images have been selected, but you also need to store the approved value for each element in your submit form. </p> <p>In Rails it is common to create hidden input elements storing additional data. Given the task at hand - they can store a value of 0/1 - rejected/approved. You can come up with your own convention for naming the image/select values.</p> <p>Then, in your view, add onclick listeners to your images pointing to a javascript function:</p> <p>1) checking if the clicked element is already selected, 2) changing the css of the current element, 3) updating the hidden input value for that element.</p> <p>Here is a dummy demonstration - <a href="http://jsfiddle.net/brj6h/2/" rel="nofollow">jsfiddle</a></p> <p>Later, you can then get the approved/rejected value from the <code>params</code> in your controller, like (see below).</p> <p>In case you want to assign a css class when constructing the view and depending on an <code>approved</code> flag value, you could do something like:</p> <pre><code> &lt;img id="image_&lt;%= image_id %&gt;" src="&lt;%= image_item.path" class="&lt;%= (image_item.approved.to_i == 1) ? 'approved_img' : 'non_appr_img' %&gt;" %&gt; &lt;input id="image_&lt;%= image_id %&gt;_app_flg" type="hidden" value="&lt;%= image_item.approved %&gt;" /&gt; </code></pre> <p>where image_item is an object properties <code>path</code> and <code>approved</code> (self explanatory), <code>image_id</code> the id of the image object, <code>approved_img</code> and <code>non_appr_img</code> - css classes.</p> <p><em>I'm not discussing the back-end for storing the approved flag, as it seems it is out of the scope of the question</em></p> <p><strong>EDIT</strong></p> <p>Brief concerning back-end</p> <p>Given the you have an images model, extend it to include an approval property (prepare a database migration and edit your model .rb file to include the new columns). </p> <p>In the view, include all of the hidden inputs inside a form which will be submitted to your controller(for example looping through an array on your images). For example:</p> <pre><code> &lt;%= form_for :images, :url =&gt; {:action =&gt; "approve_images"}, :html =&gt; {:name =&gt; "testForm"} do |f| %&gt; &lt;!-- TODO do stuff here - eg display images --&gt; &lt;% @images.each do |imageItem| %&gt; &lt;%= f.hidden_field "#{imageItem.id}_appproved", {:value =&gt; imageItem.approved}%&gt; &lt;% end %&gt; &lt;!-- TODO add a submit button --&gt; &lt;% end %&gt; </code></pre> <p>*You need to Here <code>:images</code> is the controller, <code>approve_images</code> is the function in the controller that the form will be submitted to (include in routes), <code>@images</code> is an array with images data (from your model) and that you have prepared in the controller before rendering the view. I assume the images have ids and approved property. </p> <p>This will yield in your view dom elements like this:</p> <pre><code>&lt;input id="images_IMAGEID_appproved" name="images[IMAGEID_approved]" type="hidden" value="1" /&gt; </code></pre> <p>After submitting the form, in your controller you will be able to access these values like this:</p> <pre><code>img_approved = params[:images][IMAGEID+"_approved"] </code></pre> <p>Finally you can store that value to your database. Good luck! :)</p> <hr> <p>I am omitting a lot of even more basic things, but I think the question is too broad as it is and there are plenty of resource detailing how to create,read,write a model, prepare data for the view etc. If not - please get started with <a href="http://guides.rubyonrails.org/getting_started.html" rel="nofollow">http://guides.rubyonrails.org/getting_started.html</a></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.
    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