1

超文本/安装指南向您展示了如何将超文本添加到现有的 Rails 应用程序中。我已经到了应该能够像以前一样启动旧版应用程序的地步,但是无法解析图像资源的路由。我想我只是没有正确设置路线,另一方面,我没有改变指南所说的任何内容。这是 Hyperstack 的第一次通过,感觉很明显,但它并没有屈服于我迄今为止尝试过的简单测试。

routes.rb

    Rails.application.routes.draw do

    mount Hyperstack::Engine => '/hyperstack'  # this route should be first in the routes file so it always matches

/hyperstack/hyper_component.rb

# app/hyperstack/hyper_component.rb
class HyperComponent
  # All component classes must include Hyperstack::Component
  include Hyperstack::Component
  # The Observable module adds state handling
  include Hyperstack::State::Observable
  # The following turns on the new style param accessor
  # i.e. param :foo is accessed by the foo method
  param_accessor_style :accessors
end

/components/shipment.rb

class Shipment < HyperComponent

  # param :my_param
  # param param_with_default: "default value"
  # param :param_with_default2, default: "default value" # alternative syntax
  # param :param_with_type, type: Hash
  # param :array_of_hashes, type: [Hash]
  # other :attributes  # collects all other params into a hash
  # fires :callback  # creates a callback param

  # access params using the param name
  # fire a callback using the callback name followed by a !

  # state is kept and read as normal instance variables
  # but when changing state prefix the statement with `mutate`
  # i.e. mutate @my_state = 12
  #      mutate @my_other_state[:bar] = 17

  # the following are the most common lifecycle call backs,
  # delete any that you are not using.
  # call backs may also reference an instance method i.e. before_mount :my_method

  before_mount do
    # any initialization particularly of state variables goes here.
    # this will execute on server (prerendering) and client.
  end

  after_mount do
    # any client only post rendering initialization goes here.
    # i.e. start timers, HTTP requests, and low level jquery operations etc.
  end

  before_update do
    # called whenever a component will be re-rerendered
  end

  before_unmount do
    # cleanup any thing before component is destroyed
    # note timers are broadcast receivers are cleaned up
    # automatically
  end

  render do
    DIV do
      'Shipment'
    end
  end
end

从一开始的命令行

$ bundle exec foreman start
...
ActionView::Template::Error (couldn't find file 'client_and_server-e8a2a4c02f7542e3e05c' with type 'application/javascript'
...

稍后在命令行中,

/Users/john/.rvm/gems/ruby-2.6.2/gems/bootstrap-4.3.1/assets/stylesheets
16:45:00 web.1        |   /Users/john/.rvm/gems/ruby-2.6.2/gems/bootstrap-4.3.1/assets/javascripts):
16:45:00 web.1        |     10: 
16:45:00 web.1        |     11:  
16:45:00 web.1        |     12:   // search path default is public/images
16:45:00 web.1        |     13:   = image_tag "Drayage-LongBeach.jpg", :size => "520x360" 
16:45:00 web.1        |     14:   %p
16:45:00 web.1        |     15:   %p
16:45:00 web.1        |     16: 

4

1 回答 1

0

基于它无法找到您打包的清单文件(我假设之前工作过)的事实,我怀疑您使用的是 webpacker < 4.0。

Webpacker 现在将包存储在/javascripts/js而不是仅仅/javscripts/ 和 hyperstack 安装程序假设。

保持当前版本的 Webpack

走进去config/initializers/assets.rb,你会在最后看到这样的一行

Rails.application.config.assets.paths << Rails.root.join('public', 'packs', 'js').to_s

只需删除, 'js',它将与旧的 webpacker 一起使用。

还有一条类似的线路config/environments/test.rb也需要修改才能使超规范工作。

升级 Webpack(推荐 - 你迟早要这样做)

或者你可以将 webpacker 升级到 ~> 4.0。

您需要对两个冲突都回答“Y”,然后您还必须删除.babelrc文件(如果存在),最后运行bin/webpack

  1. 更新您的 Gemfile
  2. bundle install
  3. bundle exec rails webpacker:install 对两个冲突都回答 Y
  4. .babelrc如果存在则删除
  5. bin/webpack

这应该成功地将 webpacker 升级到 4.x,现在一切都应该很好。

于 2019-06-24T20:11:07.313 回答