<br>タグ前後のテキストを取得するためのXPath
<div>
hoge
<br>
piyo
<br>
fuga
</div>
なるHTMLからhogeなどを取り出すためのXPathは以下
//div/text()[1]
--> hoge
//div/text()[2]
--> piyo
//div/text()[3]
--> fuga
次に、以下のようなHTMLで
<div>
hoge : XXXXX
<br>
piyo : YYYYY
<br>
fuga : ZZZZZ
</div>
hoge、piyo、fugaの順序などは不定で、存在も保証されていないときに、piyoを含むテキスト(piyo : YYYYY)を取り出すには
//div/text()[contains(string(), 'piyo')]
cellclickのイベントでフィールド名を取得する
Gridのcellclickイベントのコールバックは
cellclick(this, td, cellIndex, record, tr, rowIndex, e, eOpts )
とある。 クリックした列に関する情報はcellIndex(列のインデックス)しかもらえない。 これを対応する列のフィールド名(dataIndex)に変換するには以下のようにする。
init: ->
@control
'grid' :
cellclick: @onCellClick
...
onCellClick: ( grid, td, cellIndex, record, tr, rowIndex, e, eOpts )->
fieldName = grid.getColumnModel().getDataIndex(cellIndex);
....
テーブルのエンジンを変更するなど
innodbに変更したい
alter table hoges engine = innodb;
あと、トリガを設定したら以下のようなエラーが発生した。
Illegal mix of collations (utf8_unicode_ci,IMPLICIT) and (utf8_general_ci,IMPLICIT) for operation '='
一部のテーブルでCOLLATE=utf8_unicode_ciの指定がなかった。 utf8_unicode_ciに変更するには以下。
ALTER TABLE hoges CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
データベースのデフォルトも設定した。
ALTER DATABASE database_name DEFAULT COLLATE utf8_unicode_ci;
Railsを80番ポートで起動するには
macにて。
デフォルトは3000番。
(! 501)-> rails s
=> Booting WEBrick
=> Rails 3.2.13 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2014-01-16 09:42:13] INFO WEBrick 1.3.1
[2014-01-16 09:42:13] INFO ruby 1.9.3 (2013-02-22) [x86_64-darwin11.4.2]
[2014-01-16 09:42:13] INFO WEBrick::HTTPServer#start: pid=14786 port=3000
ポートを指定する場合は-p xxxまたは--port=xxx
(! 502)-> rails s --port=3001
=> Booting WEBrick
=> Rails 3.2.13 application starting in development on http://0.0.0.0:3001
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2014-01-16 09:43:24] INFO WEBrick 1.3.1
[2014-01-16 09:43:24] INFO ruby 1.9.3 (2013-02-22) [x86_64-darwin11.4.2]
[2014-01-16 09:43:24] INFO WEBrick::HTTPServer#start: pid=14824 port=3001
80番ポートで動かす場合も同じかと思ったが、エラーが出る。
(! 503)-> rails s --port=80
=> Booting WEBrick
=> Rails 3.2.13 application starting in development on http://0.0.0.0:80
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2014-01-16 09:44:19] INFO WEBrick 1.3.1
[2014-01-16 09:44:19] INFO ruby 1.9.3 (2013-02-22) [x86_64-darwin11.4.2]
[2014-01-16 09:44:19] WARN TCPServer Error: Permission denied - bind(2)
Exiting
sudoすればいける。
(! 505)-> sudo rails s --port=80
=> Booting WEBrick
=> Rails 3.2.13 application starting in development on http://0.0.0.0:80
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2014-01-16 09:46:21] INFO WEBrick 1.3.1
[2014-01-16 09:46:21] INFO ruby 1.9.3 (2013-02-22) [x86_64-darwin11.4.2]
[2014-01-16 09:46:21] INFO WEBrick::HTTPServer#start: pid=14964 port=80
ちなみに、1023まではsudoが必要で、1024以降はsudo不要のように見える。
毎回--port=80を打つのが面倒なので、80をデフォルトにする。
config/boot.rb
# Set up gems listed in the Gemfile.
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
require 'rails/commands/server'
module Rails
class Server
def default_options
super.merge({
:Port => 80
})
end
end
end
関連のデータもサーバーに送るには
基本、ここに書いてあるとおり。
http://fits.hatenablog.com/entry/20120225/1330166288
ただし、サーバにrailsを使用している場合は、accepts_nested_attributes_forを使って、PUTしたデータを一度に更新したくなる。 ので、dataのkeyの部分は"_attributes"で終わるようにしたい。
また、データを取り出す際に単にitem.dataから取得するだけだと、関連先に含まれるフィールドに意図しないものが含まれてしまうため、 Rails側でCan't mass-assign protected attributesとなってしまう。
以下のように実装したWriterを使用するといい感じになる(今のところ)
Ext.define "hoge.data.writer.NestedJsonWriter", {
extend : "Ext.data.writer.Json"
alias : "writer.nested"
getRecordData: (record, operation)->
data = @callParent(arguments)
if record.associations.length > 0
record.associations.keys.forEach (key)->
data["#{key}_attributes"] = []
assoc = record[key]()
assoc.data.items.forEach (item)->
itemData = item.getProxy().getWriter().getRecordData(item)
data["#{key}_attributes"].push(itemData)
data
}
フルスタックなJavascriptのWebフレームワーク
zipのファイルサイズ
zipを作成してクライアントに返すようなcontrollerのコードがあって、 それに対応するrspecで以下のように書いていた。 レスポンスのzipファイルが想定するサイズか確認している。
describe "zipファイル" do
before do
get(:show, { :id => 1, :format => :zip })
end
subject { response }
it '正しいZipファイルを返す' do
expect(subject).to be_success
expect(subject.content_type).to eq('application/zip')
expect(subject.body.length).to eq(266)
end
が実行するとrspecがパスしない。
Failure/Error: expect(subject.body.length).to eq(266)
expected: 266
got: 269
実行する環境(OS)によってサイズは異なるような気がする。 もともと、ファイルサイズの確認はここではいらないので、expectを削除して対応した。