Rails3 SSL
忘れないうちにメモメモ
SSLを使うのは簡単にできました。
Gemfileに以下のgemを指定します。:requireが必用なので注意を
gem 'bartt-ssl_requirement', '~>1.4.0', :require => 'ssl_requirement'
あとはconfig/environments/production.rbに以下の様にSSLを
使用したいcontroller,actionとSSLを試用する際のURLを指定します。
config.to_prepare do UsersController.ssl_required :settings, :send_confirmation_email, :update_settings UsersController.ssl_required :edit_username, update_username, :edit_password, :update_password UsersController.ssl_required :new_password, :create_password, :reset_password, :reset_password_operation SessionsController.ssl_required :new, :create RegistrationController.ssl_required :new, :create end config.after_initialize do SslRequirement.ssl_host = 'www.hoge.jp' end #
Rail3でJavaを呼び出し
ちょっと、調べたので、忘れないうちに書き込みします。
Rails3からJavaのクラスを使用する方法です。
Javaのソースは単純
package test.rjb;
class SampleClass {
public SampleClass() {
}
public String sample() {
return "Hello Java!";
}
}
こいつをJARに入れます。Ant使いました。
~
Rails3の方はconfig/environment.rbに以下を書きます。
ENV['JAVA_HOME'] = "/usr/local/jdk1.6.0_21/" ENV['LD_LIBRARY_PATH'] = "/usr/local/jdk1.6.0_21/jre/lib/"
ここでGeneratorでController作ります。
rails g Sample index
作ったJARファイルはlib/jars以下に入れます。
class SampleController < ApplicationController
def index
rjbtest_path = '/home/majima/Rails3/rjb/lib/jars/rjbtest.jar'
Rjb::load(rjbtest_path);
str = Rjb::import('test.rjb.SampleClass')
@s = str.new.sample
end
end
出来上がったControllerにindexに以下の様に書きます。
class SampleController < ApplicationController
def index
rjbtest_path = '/home/smajima/Rails3/rjb/lib/jars/rjbtest.jar'
Rjb::load(rjbtest_path);
str = Rjb::import('test.rjb.SampleClass')
@s = str.new.sample
end
end
View側にいかのように書くとHello Java!と表示されます。
<h1>Rjb</h1> <%= @s %>
Rubyで使えるstate_machineなるものを知る
Ruby on Rails3.2.0対応で書かれたror_ecommerceの
ソースコードを読んでいたらstate_machineなるものを知った。
https://github.com/drhenner/ror_ecommerce
https://github.com/pluginaweek/state_machine
早速Rails3.2.1で試してみた。
gemに以下を追加
gem 'state_machine', '~> 1.1.2'
試しにあるサイトで見つけたソースを利用するためにmodel作成
stateというstringに状態が入る
rails g model TrafficSignal state
class TrafficLight < ActiveRecord::Base
state_machine :initial => :red do
state :red
state :green
state :yellow
event :change_color do
transition :to => :red, :from => [:yellow]
transition :to => :yellow, :from => [:green]
transition :to => :green, :from => [:red]
end
end
endここでrails cで試してみた。
majima@majima:~/Rails3/state_machine$ rails c
Loading development environment (Rails 3.2.1)
ruby-1.9.2-p290 :001 > light = TrafficLight.new
=> #<TrafficLight id: nil, state: "red", created_at: nil, updated_at: nil>
ruby-1.9.2-p290 :002 > light.change_color!
(0.1ms) begin transaction
SQL (39.1ms) INSERT INTO "traffic_lights"
("created_at", "state", "updated_at") VALUES (?, ?, ?)
[["created_at", Fri, 17 Feb 2012 10:37:04 UTC +00:00], ["state", "green"],
["updated_at", Fri, 17 Feb 2012 10:37:04 UTC +00:00]]
(165.9ms) commit transaction
=> true
ruby-1.9.2-p290 :003 > light.red?
=> false
ruby-1.9.2-p290 :004 > light.green?
=> true
ruby-1.9.2-p290 :005 >
rails3.1でremotipartでAjaxファイルアップロード
Ajaxでのファイルアップロードでremotipartなるものを使って見ました。
https://github.com/leppert/remotipart
使い方は簡単でGemfileにgemを追加します。
gem 'remotipart'
次にapp/assets/javascripts/application.jsに1行追加してやります。
//= require jquery //= require jquery_ujs //= require jquery.remotipart //= require_tree .
テスト用にControllerとViewを作りました。
remotipartはActionDispatch::Http::UploadedFileをパラメータとして渡してくれます。
class SamplesController < ApplicationController
def new
@sample = Sample.new
end
def create
@sample = Sample.new
@sample.update_attributes(params[:sample])
if @sample.save
render 'create' and return
else
render 'new'
end
end
def upload
if remotipart_submitted?
file = params[:upload][:file]
orig = file.original_filename
ft = File.open(file.tempfile)
File.open(orig, 'wb') {|fn| fn.write(ft.read)}
end
respond_to do |format|
format.js
end
end
endnew.html.erb
<h1>Sample Upload</h1>
<%= form_for @sample do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.submit %>
<% end %>
<%= form_for :upload, :url => {:action => 'upload'}, :html => { :multipart => true }, :remote => true do |f| %>
<div class="field">
<%= f.label :file %>
<%= f.file_field :file %>
</div>
<div class="actions">
<%= f.submit 'Upload File'%>
</div>
<% end %>うまくアップロードできたらalertを出すようにjs.erbファイルも作りました。upload.js.erb
<%= remotipart_response do %>
// Display a Javascript alert
alert('success!');
<% end %>簡単ですね。
Rails3.1 WEBrickのproductionモードであやしげなエラー
WEBrickをproductionモードで動かすと以下のようなエラーが発生することがあります。
ActionController::RoutingError (No route matches [GET] "/assets/application-0de4153bb351a121359b0ff0c1d35e88.js"):
これはconfig/environments/production.rb中の以下の行を
falseからtrueにしてやると発生しなくなります。
config.serve_static_assets = false
Rails3.1 assetsのprecompile
RailsGuideにきちんと説明してあるけど忘れそうなので。
Rails3.1ではJavaScript,CSS,Imageをそれぞれapp/assets/javascripts,app/assets/stypesheets,app/assets/imagesに格納します。
Production環境ではこれをprecompileしてから使わないといけないのですが、CSSで画像等を扱っている場合はファイル名をxxxx.css.erbにして
.qq-upload-spinner {display:inline-block; background:
url(<% asset_path "loading.gif" %>); width:15px;
height:15px; vertical-align:text-bottom;}
background: url(<% asset_path "loading.gif" %>)
の様にasset_pathを入れておきます。
この様にしておいてprecompileします。
bundle exec rake assets:precompile