简体   繁体   中英

Where do I have to put JQuery in Ruby On Rails?

I am implementing in Ruby on Rails.What I want to do works with Jquery. What i want to do is just: I have a file_field_tag, to let the user upload a file, when the user selects a file, the submit button has been enabled. So the user can't click on the button as long as he didn't select a file.

This is the JQuery which I've written

$(document).ready(
    function(){
        $('input:file').change(
            function(){
                if ($(this).val()) {
                    $('input:submit').attr('disabled',false);
                    // or, as has been pointed out elsewhere:
                    // $('input:submit').removeAttr('disabled'); 
                } 
            }
            );
    });

and my view looks like:

<h2>Import projects and users</h2>


<% form_tag({:action => 'match'}, {:multipart => true, :id => 'file_form_id'}) do %>
<fieldset title="File">
    <legend>File</legend>
<table>
   <tr>
     <td>
      <label for="dump_file">
        Select a CSV File :
      </label>

         </td>
         <td >
           <%= file_field_tag 'file', :size => 500 %></p>
         </td>
       </tr>   
     </table>

           <%= submit_tag 'Submit', :disabled => true -%>


    <% end -%>

I think that this is correct. But where do i have to put this javascript? I really don't know where to put it. I've read to put it in application.js, but when I do this, nothing happens. The button stays disabled. Somebody who knows what I am missing?

looks like you removed those require lines:

// This is a manifest file that'll be compiled into including all the files listed below.
// Add new JavaScript/Coffee code in separate files in this directory and they'll automatically
// be included in the compiled file accessible from http://example.com/assets/application.js
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
//= require jquery
//= require jquery_ujs
//= require_tree .

The javascript you wrote goes in /public/javascripts/application.js. You also need a copy of jquery.js or jquery.min.js in /public/javascripts/ or you need to include one of the publicly available versions. Make sure jquery is loaded in your <head> before your application.js.

Example:

<%= javascript_include_tag 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js', 'application' %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM