manatee-itの備忘ログ

プログラム初心者のブログ。ruby on railsでWebサービスを作成中。(仕事が忙しくなかななか進まず) つまずいた所をなんでも記録。 環境:Ruby2.1 + Rails4.1.1 + Passenger + CentOS6.4(Apache + sqlite/MySQL)

check_box_tagでtoggle、rake routes実行でつまずいた点

■その1
ドットインストールを参考にcheck_box_tagとtoggleの機能を追加しようとしたところ、以下のエラーが発生。

ActionView::MissingTemplate (Missing template programs/toggle, application/toggle with {:locale=>[:en], :formats=>[:html, :text, :js, :css, :ics, :csv, :vcf, :png, :jpeg, :gif, :bmp, :tiff, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in:
* "/var/www/html/XXXX/app/views"
* "/usr/local/lib/ruby/gems/2.1.0/gems/devise-3.2.4/app/views"

コントローラーの記載に以下が抜けていたため、発生していた様子。
render nothing: true

renderはデフォルトのテンプレート以外の動作をさせるものと理解。

ドットインストールの動画だけを参考にしていると、自分でテキスト化する際にミスがあったり、なかなか大変。頻繁に時間が取れないけど、会員になってしまおうかな。。
本設定で行っているトグルの機能ですが、なぜかSQLiteではうまくいきませんでした。boolean型がないから?
MySQLでは、tinyint(1)となっており、試したところ正常に動作しています。

参考URL
Missing template - Railsのお部屋
Ruby On Railsでタスク管理アプリを作ってみる | jekylog
ドットインストール「Ruby on Rails 4入門」メモ - Qiita
ドットインストール Rails まとめ - gotagota日記

SQLiteからMySQLへのデータ変換
参考URL
SQLiteからMySQLへデータのマイグレーション(変換)|DB|ブログ|ノノログ



■その2
rake routesを実行したところ、以下のエラーが発生。

$ rake routes
rake aborted!
Gem::LoadError: You have already activated rake 10.4.2, but your Gemfile requires rake 10.3.2. Prepending `bundle exec` to your command may solve this.
(以降省略)

以下を実行して解消した。
bundle update rake

参考URL
Rails - 「rake db:migrate」コマンドで「rake aborted」になったら - Qiita

Windows8.1 +無線ルーターでWi-Fi ネットワーク「制限あり」

以前利用していた無線ルーターの調子が悪くなったので、新しいバッファローの無線ルータに買い換えたところ、ネットワークに制限ありとでてしまい、通信が全くできなくなった。
ネットワークIPアドレスは割り当てられるものの、pingが通らない。

[構成]
AirStation HighPower Giga WSR-1166DHP
lenovo Z480+Windows8.1

http://support.lenovo.com/jp/ja/documents/ht080645
WINDOWS 8.1WIFI アイコンに黄色い ! マークが付く - ノートブック

上記レノボのサポートURLの方法 1~方法 4を試したが、状況かわらず接続不可。

WPA2-パーソナルをWPA2-エンタープライズに変えることができるのかも不明。

方法 5のドライバーのアップデートで接続できるようになったものの、たびたび
ネットワークで「制限あり」の状態が再発している状況。

http://ztms.blog.fc2.com/blog-entry-53.html
Lenovo G500 (Windows 8.1) で無線LAN接続が「制限あり」となり通信できないのは… | いなじゅん


上記のブログサイトで紹介されていたWMMを無効化する方法を実施したところ、現状問題がない様子。

WMM
Wireless Multimedia Extensions - Wikipedia

帯域制御が行われないらしく、有効→無効に変更して測定したところ、回線速度24Mbps→15~18Mbps程度に下がってしまった。

linuxサーバ+radiko+ dropboxでサウージ!サウダージ

j-wareの「サウージ!サウダージ」をずっと聞いていたいので、
linuxサーバでradikoの番組を録音する設定を設定してみました。

環境:CentOS6

linuxサーバでradikoを録音

参考にさせていただいたURL
http://linux.matchy.net/archives/152
http://namaraii.com/archives/19670

yumリポジトリを使用するために、以下をインストール
rpmforge-release-0.5.3-1.el7.rf.x86_64
epel-release-6-8.noarch
remi-release-6.4-1.el6.remi.noarch

radiko録音スクリプトのrec_radiko.shをダウンロード

radiko録音スクリプトを利用するために、以下のパッケージをyumからインストール
swftools-0.9.1-1.el6.rf.x86_64
zziplib-0.13.62-1.el6.x86_64
libmp3lame0-3.99.3-23.el6.x86_64
fftw3-3.3.3-44.1.x86_64
rtmpdump-2.3-1.el6.rf.x86_64
librtmp-2.3-1.el6.rf.x86_64

試しにスクリプトを実行
# ./rec_radiko.sh FMJ 3 /tmp test.mp3
RTMPDump v2.3
(c) 2010 Andrej Stepanchuk, Howard Chu, The Flvstreamer Team; license: GPL
WARNING: No application or playpath in URL!
Connecting ...
WARNING: Trying different position for server digest!
INFO: Connected...
Starting Live Stream
For duration: 180.000 sec
INFO: Metadata:
INFO: StreamTitle
1096.188 kB / 180.01 sec
Download complete
./rec_radiko.sh: line 152: ffmpeg: コマンドが見つかりません

ffmpegを入れ忘れていた。

ffmpegrpmインストール

参考させていただいたURL
http://fujimi.eshi-net.org/archives/1007

以下のメッセージが出力され、なぜかうまくいかない。
# yum install ffmpeg
Loaded plugins: downloadonly, fastestmirror, security
Loading mirror speeds from cached hostfile
* base: www.ftp.ne.jp
* extras: www.ftp.ne.jp
* updates: www.ftp.ne.jp
Setting up Install Process
No package ffmpeg available.
Error: Nothing to do

何の操作で変わったかふめいですが、途中で、以下のエラーが発生。
# yum install ffmpeg
Loaded plugins: downloadonly, fastestmirror, security
Loading mirror speeds from cached hostfile
* base: www.ftp.ne.jp
* extras: www.ftp.ne.jp
* rpmforge: ftp.kddilabs.jp
* updates: www.ftp.ne.jp
rpmforge | 1.9 kB 00:00
Not using downloaded repomd.xml because it is older than what we have:
Current : Wed Jun 25 10:46:36 2014
Downloaded: Tue Jun 24 10:46:27 2014
Setting up Install Process
No package ffmpeg available.
Error: Nothing to do

以下を実行して解消。
# yum clean all

Linuxdropbox の設定

参考させていただいたURL
http://www.maruko2.com/mw/Dropbox%E3%82%92Linux%E3%81%A7%E5%88%A9%E7%94%A8%E3%81%99%E3%82%8B%E6%96%B9%E6%B3%95
http://linux.matchy.net/archives/152


$ ~/.dropbox-dist/dropboxd
このコンピュータは Dropbox アカウントにリンクされていません...
このデバイスをリンクするには、https://www.dropbox.com/cli_link?host_id=xxxxxxxxxxxxxxxxxx にアクセスしてください。
このコンピュータは Dropbox アカウントにリンクされていません...
このデバイスをリンクするには、https://www.dropbox.com/cli_link?host_id=xxxxxxxxxxxxxxxxxx にアクセスしてください。
このコンピュータは Dropbox アカウントにリンクされていません...
このデバイスをリンクするには、https://www.dropbox.com/cli_link?host_id=xxxxxxxxxxxxxxxxxx にアクセスしてください。
:
:
:
Linuxサーバ上でブラウザを開くものだと思って慌てましたが、違いました。
他のPCで上記の「https://www.dropbo....」にアクセスしてログイン
:
:
このコンピュータは Dropbox にリンクされました。ようこそ、xxxxxxxx さん。

$ dropbox.py status
Dropbox isn't running!
$ dropbox.py start
Starting Dropbox...Done!
$ dropbox.py status
インデックスを作成中...
$ dropbox.py status
同期中(残り 14 件のファイル, 残り 6 分)

14 件のファイルをダウンロード中 (6,580KB/秒、残り 6 分)
$ dropbox.py status
同期中(残り 2 件のファイル, 残り 1 分)
「2012-09-28 22.01.46.mp4」のインデックスを作成中...
「20131012_xxxxxxxx.zip」をダウンロード中 (8,692KB/秒、残り 1 分)
$ dropbox.py status
最新の状態
% dropbox.py lansync n

テストがうまくいきました。/usr/local/bin/とかに入れるべきでしょうか。
$ /etc/rec_radiko/rec_radiko.sh FMJ 1 /tmp test

サウージサウダージの時間に/etc/crontabを設定 (1分前開始、ntpd時刻同期は設定済み)
59 16 * * 7 root /etc/rec_radiko/rec_radiko.sh FMJ 55 /home/xxxxxx/Dropbox/public/radiko SAUDE_SAUDADE

番組終了後、ファイルができていた。これでスマートフォンからいつでも聞けます。

OS再起動後も動作するようにdropbox起動スクリプトの設定も実施しました。

#2018-02-25追加
ファイル管理ができていなくてドライブ使用容量が100%となっていました。容量を確保しましたが、録音がエラーとなるので、調べてみると2016/10/12からradikoの仕様が変更されていたとのこと。以下URLを参考させていただき、修正しました。
ドライブがいっぱいになったと思われる2017-08-20まではなぜか録音できていたようなのですが、、
radikoの仕様変更によるRaspberry Piでの録音失敗対応 - Qiita

hidden_fieldでログインしているユーザのIDを自動で追加

入力フォームで「タイトル」を入力してもらい、その際にユーザIDを自動でデータベースのレコードに追加したい。

Viewにhidden_fieldを記載。

# vim app/views/programs/_form.html.erb
      :
<%= f.hidden_field :uid, :value => current_user.id %>
      :
<%= f.label :title %>

<%= f.text_field :title %>
      :

コントローラの記載でuidを許可
# vim app/controllers/programs_controller.rb
      :
params[:program].permit(:title)

params[:program].permit(:title,:uid)

ログインしたユーザが登録したタイトルのみが表示されるようになった。

「Could not find a JavaScript runtime」のエラー、therubyracerをコメントアウトして対応

# rails g scaffold programs name:string
/usr/local/lib/ruby/gems/2.1.0/gems/execjs-2.1.0/lib/execjs/runtimes.rb:51:in `autodetect': Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)
from /usr/local/lib/ruby/gems/2.1.0/gems/execjs-2.1.0/lib/execjs.rb:5:in `'
from /usr/local/lib/ruby/gems/2.1.0/gems/execjs-2.1.0/lib/execjs.rb:4:in `'
from /usr/local/lib/ruby/gems/2.1.0/gems/uglifier-2.5.0/lib/uglifier.rb:3:in `require'
from /usr/local/lib/ruby/gems/2.1.0/gems/uglifier-2.5.0/lib/uglifier.rb:3:in `'
from /usr/local/lib/ruby/gems/2.1.0/gems/bundler-1.6.2/lib/bundler/runtime.rb:76:in `require'
from /usr/local/lib/ruby/gems/2.1.0/gems/bundler-1.6.2/lib/bundler/runtime.rb:76:in `block (2 levels) in require'
from /usr/local/lib/ruby/gems/2.1.0/gems/bundler-1.6.2/lib/bundler/runtime.rb:72:in `each'
from /usr/local/lib/ruby/gems/2.1.0/gems/bundler-1.6.2/lib/bundler/runtime.rb:72:in `block in require'
from /usr/local/lib/ruby/gems/2.1.0/gems/bundler-1.6.2/lib/bundler/runtime.rb:61:in `each'
from /usr/local/lib/ruby/gems/2.1.0/gems/bundler-1.6.2/lib/bundler/runtime.rb:61:in `require'
from /usr/local/lib/ruby/gems/2.1.0/gems/bundler-1.6.2/lib/bundler.rb:132:in `require'
from /var/www/html/Tabetai/config/application.rb:7:in `'
from /usr/local/lib/ruby/gems/2.1.0/gems/spring-1.1.3/lib/spring/application.rb:82:in `require'
from /usr/local/lib/ruby/gems/2.1.0/gems/spring-1.1.3/lib/spring/application.rb:82:in `preload'
from /usr/local/lib/ruby/gems/2.1.0/gems/spring-1.1.3/lib/spring/application.rb:140:in `serve'
from /usr/local/lib/ruby/gems/2.1.0/gems/spring-1.1.3/lib/spring/application.rb:128:in `block in run'
from /usr/local/lib/ruby/gems/2.1.0/gems/spring-1.1.3/lib/spring/application.rb:122:in `loop'
from /usr/local/lib/ruby/gems/2.1.0/gems/spring-1.1.3/lib/spring/application.rb:122:in `run'
from /usr/local/lib/ruby/gems/2.1.0/gems/spring-1.1.3/lib/spring/application/boot.rb:18:in `'
from /usr/local/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /usr/local/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from -e:1:in `

'

therubyracerをコメントアウトして対応
# vim Gemfile
gem 'therubyracer', platforms: :ruby

rails環境でテーブルにカラム追加

sqliteで、ALTER TABLEを実行すればカラムは追加できますが、マイグレーションファイルを使った方法で追加。

sqlite> .schema programs
CREATE TABLE "programs" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "created_at" datetime, "updated_at" datetime);

カラム追加のマイグレーションファイルを作成。
# rails g migration AddUidToPrograms uid:integer
invoke active_record
create db/migrate/20140605124613_add_uid_to_programs.rb

中身を確認
# cat db/migrate/20140605124613_add_uid_to_programs.rb
class AddUidToPrograms < ActiveRecord::Migration
def change
add_column :programs, :uid, :integer
end
end

# rake db:migrate
== 20140605124613 AddUidToPrograms: migrating =================================

    • add_column(:programs, :uid, :integer)

-> 0.0043s
== 20140605124613 AddUidToPrograms: migrated (0.0045s) ========================

sqlite> .schema programs
CREATE TABLE "programs" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "created_at" datetime, "updated_at" datetime, "uid" integer);

whenever でcron設定

環境:Ruby2.1 + Rails4.1.1 + Passenger + CentOS6.4(Apache + sqlite/MySQL(予定))

classとして作成した機能を毎日定時に実行するため、wheneverを導入してcrontab を設定。

(参考URL)
なんちゃってGeek(仮): Rails4.0でcron(バッチ処理)を動かす設定

                                                                                                                    • -

実行するファイル
# vim lib/tasks/search_task.rb


config/application.rbファイルに以下を追加。
config.autoload_paths += %W(#{config.root}/lib)


実行テスト
rails runner Tasks::SearchTask.execute

# vim Gemfile
gem 'whenever', :require => false
# bundle install

# wheneverize .

# vim config/schedule.rb
set :output, 'log/cron.log'
set :environment, :production

every 1.day, :at => '8:00 am' do
runner 'Tasks::SearchTask.execute'
end


# bundle exec whenever
# bundle exec whenever --update-crontab

# crontab -l
⇒設定を/etc/crontabに移動