【问题标题】:Rails 5 client side validations not working on HerokuRails 5 客户端验证在 Heroku 上不起作用
【发布时间】:2017-11-27 23:35:29
【问题描述】:

在开发过程中,我的客户端验证运行良好,但是当我将其推送到 Heroku 时,它们并没有运行。似乎 javascript 运行不正确。

模型仍然会验证并拒绝错误的输入。

我一直在 Google 上搜索我遇到的任何解决方案,但无济于事。我什至为生产环境预编译了我的资产并推送它们以查看是否可行,但不行。

这是我很长一段时间以来第一次使用 Rails,所以我不确定您需要查看我的代码的哪些部分来诊断问题,但我们将不胜感激。

编辑:添加模型和更多信息

我正在使用client_side_validations gem

目前我只是检查输入是否存在,然后使用正则表达式来确保它是一个 URL。

class Url < ApplicationRecord
  after_create :generate_slug
  validates :given_url, presence: true
  validates_format_of :given_url, :with => /(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?\z/

  # Convert the ID to base36 to be used as the path
  def generate_slug
    self.slug = self.id.to_s(36)
    self.save
  end

  # construct the new URL
  def display_slug
    ENV['BASE_URL'] + self.slug
  end

end

这是我认为的表格

<%= form_for @url, validate: true, remote: true do |f| %>
    <div class ="input-group">
        <label hidden></label>
        <%= f.text_field :given_url, validate: {presence: true}, :class => "form-control", :placeholder => "Give me a link..." %>
        <span class="input-group-btn">
        <%= f.submit 'Go!', :class => "btn btn-primary btn-lg" %>
        </span>
     </div> <!-- input group -->
<% end %>

和部署日志

Total 0 (delta 0), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote: 
remote: -----> Node.js app detected
remote: 
remote: -----> Creating runtime environment
remote:        
remote:        NPM_CONFIG_LOGLEVEL=error
remote:        NPM_CONFIG_PRODUCTION=true
remote:        NODE_VERBOSE=false
remote:        NODE_ENV=production
remote:        NODE_MODULES_CACHE=true
remote: 
remote: -----> Installing binaries
remote:        engines.node (package.json):  unspecified
remote:        engines.npm (package.json):   unspecified (use default)
remote:        engines.yarn (package.json):  unspecified (use default)
remote:        
remote:        Resolving node version 6.x via semver.io...
remote:        Downloading and installing node 6.11.0...
remote:        Using default npm version: 3.10.10
remote:        Resolving yarn version (latest) via semver.io...
remote:        Downloading and installing yarn (0.24.5)...
remote:        Installed yarn 0.24.5
remote: 
remote: -----> Restoring cache
remote:        Loading 2 from cacheDirectories (default):
remote:        - node_modules (not cached - skipping)
remote:        - bower_components (not cached - skipping)
remote: 
remote: -----> Building dependencies
remote:        Installing node modules (yarn.lock)
remote:        yarn install v0.24.5
remote:        [1/4] Resolving packages...
remote:        [2/4] Fetching packages...
remote:        [3/4] Linking dependencies...
remote:        [4/4] Building fresh packages...
remote:        Done in 0.07s.
remote: 
remote: -----> Caching build
remote:        Clearing previous node cache
remote:        Saving 2 cacheDirectories (default):
remote:        - node_modules (nothing to cache)
remote:        - bower_components (nothing to cache)
remote: 
remote: -----> Build succeeded!
remote:  !     This app may not specify any way to start a node process
remote:        https://devcenter.heroku.com/articles/nodejs-support#default-web-process-type
remote: 
remote: -----> Ruby app detected
remote: -----> Compiling Ruby/Rails
remote: -----> Using Ruby version: ruby-2.3.4
remote: -----> Installing dependencies using bundler 1.15.1
remote:        Running: bundle install --without development:test --path vendor/bundle --binstubs vendor/bundle/bin -j4 --deployment
remote:        Fetching gem metadata from https://rubygems.org/.........
remote:        Fetching version metadata from https://rubygems.org/..
remote:        Fetching dependency metadata from https://rubygems.org/.
remote:        Using rake 12.0.0
remote:        Using concurrent-ruby 1.0.5
remote:        Using i18n 0.8.4
remote:        Using minitest 5.10.2
remote:        Using thread_safe 0.3.6
remote:        Using builder 3.2.3
remote:        Using erubi 1.6.0
remote:        Using mini_portile2 2.2.0
remote:        Using rack 2.0.3
remote:        Using nio4r 2.1.0
remote:        Using websocket-extensions 0.1.2
remote:        Using mime-types-data 3.2016.0521
remote:        Using arel 8.0.0
remote:        Using method_source 0.8.2
remote:        Using thor 0.19.4
remote:        Using bundler 1.15.1
remote:        Using pg 0.21.0
remote:        Using puma 3.9.1
remote:        Using sass 3.4.24
remote:        Using tilt 2.0.7
remote:        Using execjs 2.7.0
remote:        Using coffee-script-source 1.12.2
remote:        Using turbolinks-source 5.0.3
remote:        Using multi_json 1.12.1
remote:        Using regexp_parser 0.4.3
remote:        Using nokogiri 1.8.0
remote:        Using websocket-driver 0.6.5
remote:        Using mime-types 3.1
remote:        Using tzinfo 1.2.3
remote:        Using rack-test 0.6.3
remote:        Using sprockets 3.7.1
remote:        Using figaro 1.1.1
remote:        Using uglifier 3.2.0
remote:        Using autoprefixer-rails 7.1.1.2
remote:        Using turbolinks 5.0.1
remote:        Using coffee-script 2.4.1
remote:        Using mail 2.6.6
remote:        Using js_regex 1.2.3
remote:        Using activesupport 5.1.1
remote:        Using loofah 2.0.3
remote:        Using rails-dom-testing 2.0.3
remote:        Using globalid 0.4.0
remote:        Using activemodel 5.1.1
remote:        Using jbuilder 2.7.0
remote:        Using rails-html-sanitizer 1.0.3
remote:        Using bootstrap 4.0.0.alpha6
remote:        Using activejob 5.1.1
remote:        Using activerecord 5.1.1
remote:        Using actionview 5.1.1
remote:        Using actionpack 5.1.1
remote:        Using actioncable 5.1.1
remote:        Using actionmailer 5.1.1
remote:        Using railties 5.1.1
remote:        Using sprockets-rails 3.2.0
remote:        Using jquery-rails 4.3.1
remote:        Using coffee-rails 4.2.2
remote:        Using rails 5.1.1
remote:        Using sass-rails 5.0.6
remote:        Using client_side_validations 9.3.3
remote:        Bundle complete! 20 Gemfile dependencies, 59 gems now installed.
remote:        Gems in the groups development and test were not installed.
remote:        Bundled gems are installed into ./vendor/bundle.
remote:        Bundle completed (2.69s)
remote:        Cleaning up the bundler cache.
remote:        Detected manifest file, assuming assets were compiled locally
remote: 
remote: ###### WARNING:
remote:        You have not declared a Ruby version in your Gemfile.
remote:        To set your Ruby version add this line to your Gemfile:
remote:        ruby '2.3.4'
remote:        # See https://devcenter.heroku.com/articles/ruby-versions for more information.
remote: 
remote: ###### WARNING:
remote:        No Procfile detected, using the default web server.
remote:        We recommend explicitly declaring how to boot your server process via a Procfile.
remote:        https://devcenter.heroku.com/articles/ruby-default-web-server
remote: 
remote: -----> Discovering process types
remote:        Procfile declares types     -> (none)
remote:        Default types for buildpack -> console, rake, web, worker
remote: 
remote: -----> Compressing...
remote:        Done: 40M
remote: -----> Launching...
remote:        Released v25
remote:        https://fathomless-plateau-97011.herokuapp.com/ deployed to Heroku
remote: 
remote: Verifying deploy... done.

【问题讨论】:

  • 你不需要在dev中预编译。预编译在 heroku 中自动完成
  • @illusionist 是的,我最初没有这样做,但我遇到了一些提示它的 stackoverflow 问题。我将删除它们只是为了保持正确。
  • 请上传您的模型验证,您可以打开 app/models/your_model.rb,并告诉我们哪些验证不起作用(更详细)
  • @widjajayd 更新了问题。

标签: ruby-on-rails ruby heroku ruby-on-rails-5


【解决方案1】:
  • 对于你的 app/assets/javascripts/application.js 确保你有

    //= 需要 rails.validations

  • 站点示例显示不需要为您的表单使用 remote: true

    • 我了解 gem 将处理 ajax 请求,因此您无需手动为 ajax 再次设置 remote: true

【讨论】:

  • 我有 remote: true 所以页面在我提交时不会重新加载,我删除它并发送表单,但它下面的视图没有填充缩短的 url。
  • 没问题老兄,谢谢你的帮助哈哈。这让我发疯了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多