Skip to content

Instantly share code, notes, and snippets.

@jk195417
Last active October 16, 2016 07:16
Show Gist options
  • Save jk195417/e3730ff9f9aa2d6e2b611f538504f17a to your computer and use it in GitHub Desktop.
Save jk195417/e3730ff9f9aa2d6e2b611f538504f17a to your computer and use it in GitHub Desktop.

Rails 開發 環境填坑之旅 (vue nokogiri)

版本: Rails 4.2 錯誤: gem Nokogiri

旅途是怎麼開始的

事情是這樣開始的,我正開心的用著 vue-rails 的 gem (裏面原本是 vue 1.0) 做些前端頁面,但是今天用的時候 chrome 的 console 報錯了...

Uncaught TypeError: Cannot read property 'indexOf' of undefined

奇怪耶!但是想達到的效果也正確的出來了,不夠身為一個有尊嚴的程式設計師,怎麼能夠允許 console 右上方有個紅色的叉叉呢!
好吧先問問萬能的G大神,找了幾篇比較相似錯誤的提問: vuejs/vue#3490
哦~原來可能是因為 vue 更新到 vue 2了有些基礎的套件升級後對 vue 1 支援比較差~~(幹可是我沒有升級 vue 2 啊!)~~。
好拉,都下坑了,來升級 vue 好了。

vue 1 至 vue 2

首先我的vue來源是Gemfile內的

gem 'vuejs-rails'

去 Github 看看 vuejs-rails 作者更新成 vue 2 了沒...

  • Package Versions : vue v2.0.1 vue-router v2.0.0 vue-resource v1.0.3

好的,在專案目錄下執行bundle update然後結束這回合吧,哈哈這次環境更新真簡單哈哈哈..哈嗚嗚嗚嗚嗚,又爆了,節錄一點點借你看。

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

current directory: /Users/Xin/.rvm/gems/ruby-2.3.0/gems/nokogiri-1.6.8.1/ext/nokogiri
/Users/Xin/.rvm/rubies/ruby-2.3.0/bin/ruby -r ./siteconf20161016-14438-d1klwj.rb extconf.rb
checking if the C compiler accepts ... yes
checking if the C compiler accepts -Wno-error=unused-command-line-argument-hard-error-in-future... no
Building nokogiri using packaged libraries.
Using mini_portile version 2.1.0
checking for iconv.h... yes
checking for gzdopen() in -lz... yes
checking for iconv using --with-opt-* flags... yes
************************************************************************
IMPORTANT NOTICE:

Building Nokogiri with a packaged version of libxml2-2.9.4.

Team Nokogiri will keep on doing their best to provide security
updates in a timely manner, but if this is a concern for you and want
to use the system library instead; abort this installation process and
reinstall nokogiri as follows:

    gem install nokogiri -- --use-system-libraries
        [--with-xml2-config=/path/to/xml2-config]
        [--with-xslt-config=/path/to/xslt-config]

If you are using Bundler, tell it to use the option:

    bundle config build.nokogiri --use-system-libraries
    bundle install

Note, however, that nokogiri is not fully compatible with arbitrary
versions of libxml2 provided by OS/package vendors.
************************************************************************
Extracting libxml2-2.9.4.tar.gz into tmp/x86_64-apple-darwin15.3.0/ports/libxml2/2.9.4... OK
Running 'configure' for libxml2 2.9.4... OK
Running 'compile' for libxml2 2.9.4... ERROR, review '/Users/Xin/.rvm/gems/ruby-2.3.0/gems/nokogiri-1.6.8.1/ext/nokogiri/tmp/x86_64-apple-darwin15.3.0/ports/libxml2/2.9.4/compile.log' to see what happened. Last lines are:
========================================================================
    unsigned short* in = (unsigned short*) inb;
                         ^~~~~~~~~~~~~~~~~~~~~
encoding.c:815:27: warning: cast from 'unsigned char *' to 'unsigned short *' increases required alignment from 1 to 2 [-Wcast-align]
    unsigned short* out = (unsigned short*) outb;
                          ^~~~~~~~~~~~~~~~~~~~~~
4 warnings generated.
  CC       error.lo
  CC       parserInternals.lo
  CC       parser.lo
  CC       tree.lo
  CC       hash.lo
  CC       list.lo
  CC       xmlIO.lo
xmlIO.c:1450:52: error: use of undeclared identifier 'LZMA_OK'
    ret =  (__libxml2_xzclose((xzFile) context) == LZMA_OK ) ? 0 : -1;
                                                   ^
1 error generated.
make[2]: *** [xmlIO.lo] Error 1
make[1]: *** [all-recursive] Error 1
make: *** [all] Error 2
========================================================================
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
	--with-opt-dir
	--with-opt-include
	--without-opt-include=${opt-dir}/include
	--with-opt-lib
	--without-opt-lib=${opt-dir}/lib
	--with-make-prog
	--without-make-prog
	--srcdir=.
	--curdir
	--ruby=/Users/Xin/.rvm/rubies/ruby-2.3.0/bin/$(RUBY_BASE_NAME)
	--help
	--clean
	--use-system-libraries
	--enable-static
	--disable-static
	--with-zlib-dir
	--without-zlib-dir
	--with-zlib-include
	--without-zlib-include=${zlib-dir}/include
	--with-zlib-lib
	--without-zlib-lib=${zlib-dir}/lib
	--enable-cross-build
	--disable-cross-build

好的,他提示我們輸入gem install nokogiri -- --use-system-libraries那就輸入看看吧

.
.
.

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    current directory: /Users/Xin/.rvm/gems/ruby-2.3.0/gems/nokogiri-1.6.8.1/ext/nokogiri
/Users/Xin/.rvm/rubies/ruby-2.3.0/bin/ruby -r ./siteconf20161016-26537-165xhja.rb extconf.rb --use-system-libraries
checking if the C compiler accepts ... yes
checking if the C compiler accepts -Wno-error=unused-command-line-argument-hard-error-in-future... no
Building nokogiri using system libraries.
Using pkg-config gem version 1.1.7
checking for libxml-2.0... no
checking for libxslt... no
checking for libexslt... no
ERROR: cannot discover where libxml2 is located on your system. please make sure `pkg-config` is installed.
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
	--with-opt-dir
	--with-opt-include
	--without-opt-include=${opt-dir}/include
	--with-opt-lib
	--without-opt-lib=${opt-dir}/lib
	--with-make-prog
	--without-make-prog
	--srcdir=.
	--curdir
	--ruby=/Users/Xin/.rvm/rubies/ruby-2.3.0/bin/$(RUBY_BASE_NAME)
	--help
	--clean
	--use-system-libraries
	--with-zlib-dir
	--without-zlib-dir
	--with-zlib-include
	--without-zlib-include=${zlib-dir}/include
	--with-zlib-lib
	--without-zlib-lib=${zlib-dir}/lib
	--with-xml2-dir
	--without-xml2-dir
	--with-xml2-include
	--without-xml2-include=${xml2-dir}/include
	--with-xml2-lib
	--without-xml2-lib=${xml2-dir}/lib
	--with-libxml-2.0-config
	--without-libxml-2.0-config
	--with-pkg-config
	--without-pkg-config
	--with-pkg-config
	--without-pkg-config
	--with-override-variables
	--without-override-variables
	--with-xslt-dir
	--without-xslt-dir
	--with-xslt-include
	--without-xslt-include=${xslt-dir}/include
	--with-xslt-lib
	--without-xslt-lib=${xslt-dir}/lib
	--with-libxslt-config
	--without-libxslt-config
	--with-exslt-dir
	--without-exslt-dir
	--with-exslt-include
	--without-exslt-include=${exslt-dir}/include
	--with-exslt-lib
	--without-exslt-lib=${exslt-dir}/lib
	--with-libexslt-config
	--without-libexslt-config

幹!

不過離問題越來越近了,關鍵字:

  • checking for libxml-2.0... no
  • checking for libxslt... no
  • checking for libexslt... no

再問問G大神,果然被坑的人不只是我:SlatherOrg/slather#227

最後照著網友的解決方法:

  1. 先開啟xcode,太久沒開進去更新一下按個確定。

  2. 輸入下面指令重新安裝壞掉的gem

gem uninstall nokogiri
xcode-select --install
gem install nokogiri

成功拉!可喜可賀

故事的結尾

立馬重啟 Rails app 看看我那小紅x被解決了沒

bundle install
rails s

chrome -> http://localhost:3000/ -> 檢查

.
.
.
.
.

[Vue warn]: failed to compile template:

<div id="youtube" class="col-md-8 col-md-offset-2">
  <h1>{{src.items[7].snippet.channelTitle}}</h1>

  <div v-for="item in src.items | orderBy item.snippet.publishedAt 1" class="embed-responsive embed-responsive-16by9 youtube_video">
    <iframe v-if="item.id.kind=='youtube#video'" :src="'https://www.youtube.com/embed/'+item.id.videoId" class="embed-responsive-item" frameborder="0" ;="" allowfullscreen=""></iframe>
  </div>
</div>

- invalid expression: v-for="item in src.items | orderBy item.snippet.publishedAt 1"

 (found in root instance)

夠了!我要先跟你冷戰30分鐘等等再回來把你修成 vue 2 的寫法

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment