samedi 21 mars 2015

rails 4.1.6 form_for coffeescript on multiple views not working


I can get the coffeescript to work with one view (/users/new.html.erb):



<h1> Sign up! </h1>
<%= form_for (@user) do |f| %>
<%= f.label :email %><p class="error email-error"></p>
<%= f.email_field :email %>

<%= f.submit "Sign Up", class: 'btn' %>
<% end %>


and one coffeescript file (/users.js.coffee):



ready = ->
$('form').submit ->

$('.error').text ''
email = $('#user_email').val()

if email == ''
$('.email-error').text 'Please enter your email.'

false
return

$(document).ready(ready)
$(document).on('page:load', ready)


BUT not when I try to add a second view (/sessions/new.html.erb):



<h1> Log In </h1>
<%= form_for(:session, url: login_path) do |f| %>
<%= f.label :email %><p class="error email-error"></p>
<%= f.email_field :email, :id => "session_email" %>

<%= f.submit "Log In", class: 'btn' %>
<% end %>


and coffeescript file (/sessions/js.coffee):



main = ->

$('form').submit ->

$('.error').text ''
email = $('#session_email').val()

if email == ''
$('.email-error').text 'Please enter your email'

false
return

$(document).ready(main)
$(document).on('page:load', main)


I know that ultimately the coffeescript is compiled into one file by rails so it will not know which 'form' I am referencing. But when I've tried to add id tags to the forms, I can't even get one form to work. I've been stuck for 2 days on this and can't figure it out. Any help would be greatly appreciated. Thanks!





Aucun commentaire:

Enregistrer un commentaire