简体   繁体   中英

jQuery won't work with simple_form in Rails 4

I'm trying to just include some basic jQuery in a Simple_form generated scaffold in Rails to hide certain elements. For reasons I don't understand, jQuery doesn't seem to be working at all. The layout file has the correct javascript include tag. I'm wondering if I'm just making a basic CSS/jQuery error or if there is something going on with Rails I need to fix.

Here is the form:

<div class = "check1" >
    <%= f.input :use_current_location?, input_html: {checked: true } %>
  </div>
  <div class = "hidden1">
    <%= f.input :address_line_1 %>
    <%= f.input :address_line_2 %>
    <%= f.input :address_line_3 %>
    <%= f.input :city %>
    <%= f.input :postcode %>
    <%= f.input :country %>
  </div>
  <div class = "check2">
    <%= f.input :available_now?, input_html: {checked: true } %>
  </div>
  <div class = "hidden2">
    <%= f.input :available_from? %>
    <%= f.input :time %>
  </div>

  <div class="actions">
    <%= f.button :submit %>
  </div>
<% end %>

I'm just trying to hide certain classes of the form. The jQuery is:

$(document).ready(function() {

    $(".hidden1").hide(); //Initially hides selected classes.
    $(".hidden2").hide();
});

I am wondering if it is something to do with the js tag. Layout file:

<!DOCTYPE html>
<html>
<head>
  <title><!-- YOUR APP NAME--></title>
  <%= stylesheet_link_tag    "application", :media => "all" %>
  <%= javascript_include_tag "appication" %>
  <%= csrf_meta_tags %>
</head>
<body>

<%= render :partial => ('layouts/navbar') %>
<%= yield %>

</body>
</html>

Any help would be appreciated. If anyone can also recommend a different form generator that would also be great.

I am running Rails 4/Ruby 2.0.0

Missing the closing parenthesis on the last line. Try });

After testing out the same code in different projects it appears to be a problem with simple_form overriding the javascript files. I have found that the same thing happens using formtastic. I still don't know why this is but the issue can be avoided by not using a form generator and using the default scaffold.

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