问题
I installed gem 'client_side_validations', run bundle and run rails g client_side_validations:install
Installation DIDN"T create any js files, so I created them and put code from HitHub in my assets/javascript folder.
added line into application.js
//= require rails.validations
added line into application.html.erb
<%= javascript_include_tag :defaults, "rails.validations", "rails.validations.custom" %>
and validation isn't working online(using Devise gem)
In my view:
<%= form_for resource, ... , :validate => true do |f| %>
In GoogleChrome console logs:
Failed to load resource: the server responded with a status of 404 (Not Found)
"/assets/javascripts/rails.validations.custom.js"
"/assets/javascripts/rails.validations.js"
What I missed ?
回答1:
When you run the command rails g client_side_validations:install
it seems to only generate the initializer config/initializers/client_side_validations.rb
.
However, on the github page under the "Install" section they mention the command for copying the javascript file to the project's assets: rails g client_side_validations:copy_assets
Note: If you run Rails >= 3.1 you don't need to add the javascript file in the layout anymore, it should be sufficient to have <%= javascript_include_tag "application" %>
as long as the application.js contains the line //= require_tree .
which includes all javascript files under app/assets/javascripts
directory.
In the initializer client_side_validations.rb
file you also need to uncomment the last block:
# Uncomment the following block if you want each input field to have the validation messages attached.
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
...
end
Once you have done this you need to restart the server for the changes to take effect.
来源:https://stackoverflow.com/questions/11029197/cant-include-js-file-for-client-slide-validation