ラベル Rails の投稿を表示しています。 すべての投稿を表示
ラベル Rails の投稿を表示しています。 すべての投稿を表示

2010-10-13

javascriptでinput textの内容を表示

[例]以下のテキストボックスに値(xxxx)を表示するとき。javascript文は以下のとおり。
<input id="store_longitude" name="store[longitude]" size="30" type="text" /> 

[javascript文]
 <script type="text/javascript"> 
 document.getElementById("store_longitude").value = xxxx;
 </script>

2010-10-11

google map マウスホイールで拡大縮小

[目的]google map において、マウスホイールで拡大縮小できるようにする。

以下より参照
GoogleMapsをマウスホイールで拡大・縮小できるようにする

map.enableScrollWheelZoom();

google map 緯度経度取得

[目的]google mapでクリック地点の緯度経度を取得する。

以下から引用
geekなページ

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Google Maps JavaScript API Example - simple</title>
    <script src="http://maps.google.com/maps?file=api&v=2&key=aaaaa"
        type="text/javascript" charset="utf-8"></script>
  </head>
  <body>
    <div id="map" style="width: 300px; height: 300px"></div>
    <script type="text/javascript">
    //<![CDATA[

    if (GBrowserIsCompatible()) {
      var map = new GMap2(document.getElementById("map"));
      map.setCenter(new GLatLng(37.441944, -122.141944), 13);
    }

    GEvent.addListener(map, 'click', function(overlay, point) {
      if (point) {
        document.getElementById("show_x").innerHTML = point.x;
        document.getElementById("show_y").innerHTML = point.y;
      }
    });

    //]]>
    </script>

    <P id="show_x"></P>
    <P id="show_y"></P>

  </body>

</html>

railsでgoogle mapを表示

[目的]rails3.0でgoogle mapを表示する。
[環境]rails3.0 + ruby1.8.7

(1)プロジェクト作成
rails new googlemap

(2)コントローラ作成
rails g controller maps index

(2)index.html.erbを編集。
    <div id="map" style="width: 500px; height: 500px"></div>
    <script type="text/javascript">
    //<![CDATA[
    if (GBrowserIsCompatible()) {
      var map = new GMap2(document.getElementById("map"));
      map.addControl(new GLargeMapControl());
      map.addControl(new GMapTypeControl());
      map.setCenter(new GLatLng(38, 138), 5);
      map.disableDragging();
    }
    GEvent.addListener(map, 'click', function(overlay, point) {
      if (point) {
          alert("x = " + point.x + ", y = " + point.y);
          map.openInfoWindow(point,
          document.createTextNode("openInfoWindow Test"));
      }
    });
    //]]>
    </script>
(3)application.html.erbを編集。
script srcを追加。
<!DOCTYPE html>
<html>
<head>
  <title>Googlemap</title>
  <%= stylesheet_link_tag :all %>
  <%= javascript_include_tag :defaults %>
  <%= csrf_meta_tag %>
  <script src="http://maps.google.com/maps?file=api&v=2&key=ABQIAAAABWYMASZTPMzQMXu2alSa6xTvcRCYKSpWpPqDL-sNMuxVdHFI_BTd8zF9kdpV97QhhvGd0NWsQckXsw"
      type="text/javascript"></script>
</head>
<body>

<%= yield %>

</body>
</html>
(4)サーバ起動
rails s

[関連エントリ]
GoogleMapでマッシュアップの練習(Rails編) #2

2010-10-09

file_columnでファイルのアップデート

[目的]画像のアップデート
[環境]Rails3.0, ruby1.8.7

(1)ImageMagickのインストール
 
sudo port install ImageMagick

--あとは、関連エントリのそのまま実行

(2)プロジェクト作成

rails new file_column_test

(3)file_columnプラグインインストール


cd file_column_test
rails plugin install git://github.com/tekin/file_column.git


(4)パッチ適用

vendor/plugins/file_column/lib/file_column.rb
Rails.rootを変更。


--- file_column.rb.orig 2010-09-28 00:54:44.000000000 +0900
+++ file_column.rb 2010-09-28 00:55:12.000000000 +0900
@@ -594,7 +594,7 @@

# default options. You can override these with +file_column+'s +options+ parameter
DEFAULT_OPTIONS = {
- :root_path => File.join(RAILS_ROOT, "public"),
+ :root_path => File.join(Rails.root, "public"),
:web_root => "",
:mime_extensions => MIME_EXTENSIONS,
:extensions => EXTENSIONS,



(5)Gemfile修正

gem 'rmagick', :require => 'RMagick'

(6)bundle installを実行

bundle install

(7)scaffold雛形作成
rails g scaffold Entry name:string image:string
rake db:migrate

(8)model修正
app/models/entry.rb



class Entry < ActiveRecord::Base
file_column :image,
:root_path => "#{Rails.root}/public/store",
:web_root => "store/",
:magick => {
:versions => {
:thumb => { :crop => "1:1", :size => "50x50" },
:middle => "150x150>",
:large => "480x480>"
}
}
end

(9)viewを修正。

app/views/entries/_form.html.erb
multipart、file_column_fieldに変更。


<%= form_for(@entry, :html => { :multipart => true }) do |f| %>
<% if @entry.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@entry.errors.count, "error") %> prohibited this entry from being saved:</h2>

<ul>
<% @entry.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :image %><br />
<%= file_column_field "entry", "image" %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>


(10)view修正2
app/views/entries/show.html.erb
image_tagに変更。


<p id="notice"><%= notice %></p>

<p>
<b>Name:</b>
<%= @entry.name %>
</p>

<p>
<b>Image:</b>
<%= image_tag(url_for_image_column(@entry, "image", :middle)) if @entry.image %>
</p>


<%= link_to 'Edit', edit_entry_path(@entry) %> |
<%= link_to 'Back', entries_path %>


(10)view修正3
app/views/entries/index.html.erb
image_tagに変更。

<p id="notice"><%= notice %></p>

<p>
<b>Name:</b>
<%= @entry.name %>
</p>

<p>
<b>Image:</b>
<%= image_tag(url_for_image_column(@entry, "image", :middle)) if @entry.image %>
</p>


<%= link_to 'Edit', edit_entry_path(@entry) %> |
<%= link_to 'Back', entries_path %>



(11)サーバ起動
rails s

<関連エントリ>
篳篥日記 [Rails] ruby 1.9.2 + Rails 3.0 + file_column
http://d.hatena.ne.jp/hichiriki/20100927

2010-10-02

[Rails]migrationのやり方

(1)migrationファイルの作成
script/generate migration change_stores_latitude_to_float

(2)migrationファイルの修正

[コマンド一覧]
create_table(name, options)
drop_table(name)
rename_table(old_name, new_name)
add_column(table_name, column_name, type, options)
rename_column(table_name, column_name, new_column_name)
change_column(table_name, column_name, type, options)
remove_column(table_name, column_name)
add_index(table_name, column_name, index_type)
remove_index(table_name, column_name)

(3)migrate実行

[参考]
http://tech.feedforce.jp/railsmigration.html
http://ponk.jp/?p=2374

2009-05-26

rails2.0でscaffoldを実行する。


$ ruby script/generate scaffold person name:string age:integer


<参考/引用>
[技術][Ruby][Ruby on Rails]Rails 2.0のscaffoldを使ってみた

macOS X10.5.7にruby1.8.7+rails2.3.2をインストールする!

[目的]
・macOSX 10.5.7にruby1.8.7+rails2.3.2の開発環境を構築する。
 (デフォルトruby 1.8.6なのでruby1.8.7をソースからインストールする。)

[環境]
Rails 2.3.2
Ruby 1.8.7
macOSX 10.5.7

[作業方法]
(1)XcodeTools他のインストール。
 (付属のMac OS X Install Disc2 からインストール)
 ※インストール画面にて、WebObjectsをチェックし、
  追加でインストールする。

(2)環境変数の設定

%export PATH=/usr/local/bin:/opt/local/bin:/opt/local/sbin/:$PATH
%export MANPATH=/opt/local/man:/opt/local/share/man:$MANPATH

(3)Ruby1.8.7のインストール
 1.8.7をソースからコンパイルして/usr/localにインストールする
http://www.ruby-lang.org/ja/downloads/

% curl -O ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.7-p72.tar.gz
% tar zxvf ruby-1.8.7-p72.tar.gz
% cd ruby-1.8.7-p72
% ./configure --prefix=/usr/local --enable-shared --enable-pthread
% make
% sudo make install
% make clean


% ruby -v
ruby 1.8.7 (2008-08-11 patchlevel 72) [i686-darwin9.4.0]
% which ruby
/usr/local/bin/ruby

(4)gemのインストール

http://rubyforge.org/frs/?group_id=126

% ftp http://rubyforge.org/frs/download.php/45905/rubygems-1.3.1.tgz
% tar zxvf rubygems-1.3.1.tgz
% cd rubygems-1.3.1
% sudo ruby setup.rb


% gem -v
1.3.1
% which gem
/usr/local/bin/gem

(4)rails 2.3.2をインストール


% sudo gem install rails

% rails -v
Rails 2.3.2
% which rails
/usr/local/bin/rails

(5)sqlite3をインストール

SQLiteのインストール
% port install sqlite3

sqlite3-rubyのインストール
% gem install sqlite3-ruby


(6)bash_profileの設定
※再起動後、rubyパスが1.8.6に
 戻っていたため、上記設定を実行。

% echo 'export PATH=/usr/local/bin:$PATH' >> ~/.bash_profile


<参考/引用>
Mac OS X 10.5.6(Leopard) にRuby on Rails インストール
Mac OS X (10.4/Tiger)でSQLiteを使う際の注意点
Ruby Install Guide::MacOS X

2009-02-11

RailsのActionMailerによるメール送信

[目的]
Gmailのsmtpを利用して、メールを送信する。

[環境]
Rails 2.2.2
ActionMailer 2.2.2
Ruby 1.8.7
ubuntu 8.04

[作業方法]
(1)TLSライブラリのインストール(gmailのsmtp認証用)
 ◆フォルダ
vendor/plugins/action_mailer_tls
vendor/plugins/action_mailer_tls/lib

◆ファイル
  vendor/plugins/action_mailer_tls/init.rb

  init.rbの中身

require_dependency 'smtp_tls'


  vendor/plugins/action_mailer_tls/lib/smtp_tls.rb

smtp_tls.rbの中身

require "openssl"
require "net/smtp"

Net::SMTP.class_eval do
private
def do_start(helodomain, user, secret, authtype)
raise IOError, 'SMTP session already started' if @started
check_auth_args user, secret if user or secret

sock = timeout(@open_timeout) { TCPSocket.open(@address, @port) }
@socket = Net::InternetMessageIO.new(sock)
@socket.read_timeout = 60 #@read_timeout
@socket.debug_output = STDERR #@debug_output

check_response(critical { recv_response() })
do_helo(helodomain)

raise 'openssl library not installed' unless defined?(OpenSSL)
starttls
ssl = OpenSSL::SSL::SSLSocket.new(sock)
ssl.sync_close = true
ssl.connect
@socket = Net::InternetMessageIO.new(ssl)
@socket.read_timeout = 60 #@read_timeout
@socket.debug_output = STDERR #@debug_output
do_helo(helodomain)

authenticate user, secret, authtype if user
@started = true
ensure
unless @started
# authentication failed, cancel connection.
@socket.close if not @started and @socket and not @socket.closed?
@socket = nil
end
end

def do_helo(helodomain)
begin
if @esmtp
ehlo helodomain
else
helo helodomain
end
rescue Net::ProtocolError
if @esmtp
@esmtp = false
@error_occured = false
retry
end
raise
end
end

def starttls
getok('STARTTLS')
end

def quit
begin
getok('QUIT')
rescue EOFError
end
end
end


(2)config/environments/development.rbにSMTPの設定追加

delevopment.rbの最後に追記

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => 'smtp.gmail.com',
:port => 587,
:authentication => :plain,
:user_name => 'xxxxxx@gmail.com',
:password => '******'
}


(3)ActionMailer modelの生成

>script/generate mailer NotifyMailer result


(4)NotifyMailer modelの修正

def result(sent_at = Time.now)
subject 'NotifyMailer#result'
recipients 'xxxx@xxxxx.xxxx' #とりあえずここ修正
from 'xxxx@xxxxxx.xxx' #とりあえずここ修正
sent_on sent_at
body :greeting => 'Hi,'
end

(5)○○controllerにアクションの記述


def mail
mail = NotifyMailer.create_result()
NotifyMailer.deliver(mail)
end

(6)送信テスト
http://localhost:3000/xxx/mailにて送信出来るか確認。


[参考]
Ruby on Rails/ActionMailerでTLSを使ったメール送信

2009-01-14

Gedit をtextmate風にアレンジ

Linux(Ubuntu)でRailsの開発環境はどのようにしていますか?

NetBeansはなんか重くてイヤだし、
Emacsは挫折したし、vimでプログラムは・・って感じなんですよ。

軽くて簡単に使えるIDEないかなって探してたところ、
Macではtextmateが良いみたいなことを発見。

linuxでは、geditをtextmate風にアレンジ
出来るそうですね.

http://warlockhandler.blogspot.com/2008/04/converting-gedit-into-textmate.html


※ちなみにWindowsでは、e-text editorらしいよ。