Ȥ Blog

Ruby,C#

RubyOnRails

Ruby1.9.2 Rails2.3 MySQLencodingȥ֥

Ruby1.9.2ʸ󤬥󥳡ɾĤ褦ˤʤä褦ʤΤǤRailsǤ褯ѤMySQLɥ饤С̤бΤᤫActiveRecordǼǡʸϤ٤ASCII8ȤưƤޤ褦Ǥ

ĤΤViewǥ顼¿ȯƤޤƺä Ĵ٤Ƥߤ gem mysql2 Υ󥳡ˤбƤ餷

®󥹥ȡ뤷Ƥߤ롣

https://github.com/brianmario/mysql2

嵭ȤActiveRecordιܤɤǤߤȡRails3.1̤ξϥС0.2ϤȤȤΤȤʤΤǡenvironment.rb

config.gem "mysql2", :version => "0.2.11"
Ȥơ
rake gems:install
database.yml
development:
  adapter: mysql2
  database: hoge
  encoding: utf8
Ȥ̵˥ޥХʸⰷ褦ˤʤޤ ˤƤRuby1.8ϤΥ1.9˹碌뤿ˤϡ̤ΥեƬ˥󥳡ɤ򼨤ޥåȤɵʤʤʤ¾ˡʤΤʡ

Rails3ActiveScaffold

ActiveScaffoldϤĤˤʤäRails3бΤʡ ʤƤäȻפäƤ顢ȤäFork줿ץȤäơ餬Rails3бƤޤ

https://github.com/vhochstein/active_scaffold

ưǧΤñʥץäƤߤ롣

#railsץȤ
rails new active_scaffold_test
cd  active_scaffold_test
bundle install

#ActiveScaffoldץ饰򥤥󥹥ȡ
  rails plugin install git://github.com/vhochstein/active_scaffold.git -r 'rails-3.0'

#gemǥ󥹥ȡ뤷
gem "active_scaffold_vho" 

#ActiveScaffoldΥåȥå 
rails g active_scaffold_setup jquery  #ɸPrototypeȤϺǸjqueryλ̵

#ХɤȥХɥС褦ʤΤäƤߤ
rails g active_scaffolf BandGroup name:string
rails g active_scaffolf BandMember name:string instrument:name band_group:references
rake db:migrate

#С¹
rails s

ʴǤǤޤ

Rails3Raisl2.x˥ȥ֤̾դ

Rails2.x κϤ褯ϤΥڡ
admin/users
Ȥ
admin/products
ߤ˥ȥ˼ͳ֤̾ĤƤäƤɡ
Rails3ƱȤȤ롼ƥ󥰥顼ܤ롣

Routes.rb򳫤Ƥߤ

namespace :admin do
  resources :products
end


ߤʽΥץ뤬롣
Ǥ⤳äƤRESTFULʻȤǤʤ
Action̾ȤͳˤĤϤɤФΤ

֤̾ʤǰRaisl2.xդˤϡñ˥ǥեȤǥȥȤƤ
#match ':controller(/:action(/:id(.:format)))'
ιԤ褵Ф褫äɡ㤢Ʊͤˤ嵭ν֤̾ǤäƤФΤʡ

namespace :admin do
  match ':controller(/:action(/:id(.:format)))'
end


󡢤

ǡ

match ':controller(/:action(/:id(.:format)))', :controller => /admin\/[^\/]+/

ɽб뤳Ȥˡ

¾ˡʤΤ
äƤRESTFuläƤʤˤΡ
̿̾ɲä뤿Ӥ
Routes.rb˵ҲäΤäƤݤȻפΤϻʤǤ礦
new,create,edit,update,destroyʳΥäߤʤȤ¿ɡ

ʤ󤫤ɡ 
ܵRailsASP.Net MVC ǺꤿʤäƤ
 

KindleīСμưۿ

٣ǯ餤RubyȤäƼʬ˥᡼ۿ뤽ʥץȤäΤǤKindle˳Ƥ줿ѤΥ᡼륢ɥ쥹ۿФ褯ʤ äƤȤǤäȲ
Kindleξϥ᡼źեեȤKindle˼ǡʤФʤʤΤǡäȤäơȤcron7:10(7:00Ȥޤ˹Ƥʤ礬ˤ˵ư褦ꡣ

ͭ͡ޤǻ٤ޤ

̣ΤϼƤߤȤȻפޤ
Сб褦ʡ 

 IMG_0481

Rails3 mongrel

ʤRails3ˤǡ
rails server
äƤȡmongrel󥹥ȡ뤵Ƥˤؤ餺webrickư롢

Ĵ٤Ƥߤ顢
Gemfileʤޤˤ
gem 'mongrel'

ȤǤ餷

3Ϥʤ󤫿ѤäƤʡ 
 

Rails: postgresǤϤޤ rake aborted! libpq.so.5: cannot open shared object file

ޤ
gem install pg 
Ǥ롣

gem install pg -- --with-pg-config=/usr/local/pgsql/bin/pg_config

ǥ󥹥ȡϤǤɡrake db:create 褦Ȥ顢٤ɽΥ顼Ф롣

libpq.so.5
ĤʤȸäƤ褦Ǥ

Ǥ
/usr/local/pgsql/lib
ˤϤȤ롣
äƤȤñ˥ѥ̤äƤʤǤ⡢̤ƤϤʤɤʡ̤Υޥ󤫡

.bash_profileȤ .zshrcȤ

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/pgsql/lib

ǤȤǤޤ

ɤѥ̤˺ǥϥޤ뤳Ȥ¿

Rails3 RestfullʥѤǤ褦ˤ

Rails3Ǥ routes.rbΤ褦ˤʤäƤRestfullʪƤ٤ʤ褦ˤʤäƤޤľ̾Ȥͳ˻ȤΤǡȳƻȤ褦ˤޤ

  # This is a legacy wild controller route that's not recommended for RESTful applications.
  # Note: This route will make all actions in every controller accessible via GET requests.
  # match ':controller(/:action(/:id(.:format)))'

äǤ

jQueryUIĶñAutoCompleteƤߤ

פ֤jQueryUIΥȤ򸫤Ƥߤ顢ǽȤAutoCompleteʤΤɲäƤޤٻŻǤȤʤΤǡäƤߤ뤳Ȥˡ
Rails3ǥץȤФʤΤǤȤꤢmainȤ̾ǥǥեȤΥȥ롣
  #rails g controller main
  create  app/controllers/main_controller.rb
  invoke  erb
  create    app/views/main
  invoke  test_unit
  create    test/functional/main_controller_test.rb
  invoke  helper
  create    app/helpers/main_helper.rb
  invoke    test_unit
  create      test/unit/helpers/main_helper_test.rb
ȤꤢСư main ȥ˥Ƥߤȡʥڡʤ衼äƤʴRouting Errorˤʤ롣 ޤǤϲʤƤɤʡRals3Ϥ롼ƥ󥰥ե˽񤭹ɬפΤ Ĵ٤ʤȤ褯狼ʤΤǡȤꤢ routes.rbǥեȤΥȥȥ main/index ˤʤ褦ѹ
  # You can have the root of your site routed with "root"
  # just remember to delete public/index.html.
  # root :to => "welcome#index"
  root :to => "main#index"
app/views/main/index.html.erb ˤϡjqueryUIautocompleteΥɥȤ˽äƤʴǥץ񤤤Ƥߤ롣 ȤꤢǡJavaScriptľ񤭤ǻƤߤ롣
	<input type="text" id="textbox1"></input>
	<script type="text/javascript" charset="utf-8">
		$(function(){
			var dataItems = ["C#", "C++", "C", "Java", "JavaScript", "Ruby", "IronRuby", "JRuby"];

			$("#textbox1").autocomplete({
				source : dataItems
			});
		})
	</script>
ڡɽƤߤ롣

45

ʤñˤǤƤޤޤ Ǥ⡢ºݤˤϥС̿Τۤ¿Ȼפޤ ξ source ץ JSONǡ֤URLꤷƤơС¦Ǥϡ ʸ󤫡⤷ { label : 'ɽ', value : 'Value'}֤褦˼Ƥɤ褦Ǥ
桼Ϥʸ term Ȥѥ᡼̾ǥСϤΤǡĤ򥭡ɤȤƥե륿󥰤褦ʴˤʤ뤫Ȼפޤ
DB򤤤ѰդΤݤäΤǡ餫ǻץߥ󥰸̾ΰ桼Ϥʸ˳ΤJSON֤褦ʽ񤤤Ƥߤޤ
	class MainController < ApplicationController
	  def index
			#ʤ󤫤ޤ롼ƥ󥰤ʤΤ꤯ԤΤΤ indexajaxꥯȤɤǽʬƤޤä
	    if request.xhr?
	      data_items = %w(C# C++ C Java JavaScript Ruby IronRuby JRuby)
	      return render :json => data_items.find_all{|lang| lang.include?(params[:term]) }
	    end
	  end
	end
JavaScript sourceץURLѹ
	<input type="text" id="textbox1"></input>
	<script type="text/javascript" charset="utf-8">
		$(function(){
			//var dataItems = ["C#", "C++", "C", "Java", "JavaScript", "Ruby", "IronRuby", "JRuby"];
			$("#textbox1").autocomplete({
				//source : dataItems
				source : "/"
			});
		})
	</script>
󡢤ʤƴñʤ

31

ϥɥ뤵ʤä㳰᡼ΤƤץ饰 Exception Notifier

ʥץ饰󤬤뤽Ǥ
RailsܤKindleɤǤ餽ʪ衼ƤʤȤ񤤤Ƥޤ

ϡrescue_action_in_public Ȥȼ˼ƤꤷΤǤ޳ѤʤΤǥץ饰ȤȤˤ褦

https://github.com/rails/exception_notification
嵭ǿǤʤΤǤRails2.3ϤʤΤǡ

http://github.com/rails/exception_notification/tree/2-3-stable
äɡ
ʤ ./script/plugin install ǤϾ꤯ʤäΤǡ
Download󥯤tar.gzեȤľ
vendor/plugin/Ŭ̾ ˲षޤ

READMEδŪʻˡɤǤߤ롣

== Usage

First, include the ExceptionNotifiable mixin in whichever controller you want
to generate error emails (typically ApplicationController):

  class ApplicationController < ActionController::Base
    include ExceptionNotification::Notifiable
    ...
  end

Then, specify the email recipients in your environment:

  ExceptionNotification::Notifier.exception_recipients = %w(joe@schmoe.com bill@schmoe.com)

And that's it! The defaults take care of the rest.


פApplicationControllerƬ

    include ExceptionNotification::Notifiable

Ƥäơconfig/environment.rbǤconfig/initializers ŬʥեäƤΤʤǡ

  ExceptionNotification::Notifier.exception_recipients = %w(Υ᡼륢ɥ쥹)

äƤƤOK餷

ActionMailerѤߤǥ᡼ǤĶˤ뤳ȤǤ
ξGMailΤActionMailerϤʴˤʤäƤޤ


ActionMailer::Base.smtp_settings = {
  :enable_starttls_auto => true,
  :address => "smtp.gmail.com",
  :port => 587,
  :domain => "hogehoge",
  :authentication => :plain,
  :user_name => "Gmail",
  :password => "Gmailѥ"
}  

ǡ®虜㳰ꤲ륢äƥ᡼뤬֤ɤ¸Ƥߤ롣

HogeController < ApplicationController 
  def index
    raise '虜ȥ顼'
  end
end
󡢤ȤФʤȤ᡼ƤФ줿פʤ
development⡼ɤǵưƤ뤫餫ʡ Ȥ⤤
./script/server -e production
֥⡼ɤǼ¹ԤƤߤ롣

Net::SMTPFatalError (555 5.5.2 Syntax error. t14sm2087499icd.10
):
  /home/satoyan/.rvm/rubies/ruby-1.8.7-p330/lib/ruby/1.8/net/smtp.rb:942:in `check_response'
  /home/satoyan/.rvm/rubies/ruby-1.8.7-p330/lib/ruby/1.8/net/smtp.rb:911:in `getok'
  /home/satoyan/.rvm/rubies/ruby-1.8.7-p330/lib/ruby/1.8/net/smtp.rb:826:in `mailfrom'
  /home/satoyan/.rvm/rubies/ruby-1.8.7-p330/lib/ruby/1.8/net/smtp.rb:653:in `sendmail'
  /home/satoyan/.rvm/rubies/ruby-1.8.7-p330/lib/ruby/1.8/net/smtp.rb:526:in `start'

ʤ󤫤ʥ顼ǤƤޤ

Ĵ٤ƤߤNet::SMTPΥ饤֥ѹä褦Ԥ
senderName<hogehoge@hoge>
ߤʽ򤹤ȤΥ顼ˤʤ褦Ǥ

ǡԤäƤɤäꤹΤĴ٤餳README˽񤤤Ƥޤ

  # defaults to exception.notifier@default.com
  ExceptionNotification::Notifier.sender_address =
    %("Application Error" <app.error@myapp.com>)

ϤꡢǥեȤǥΤ褦Ǥ
ʤΤ
 ExceptionNotification::Notifier.sender_address = 'hogehoge@hoge'
Ƚ񤭴ƺټ¹ԡ

ȥ᡼뤬Ӥޤ

ǡ᡼ˤϤɤʾ󤬤뤫Ȥȡ

By default, the notification email includes four parts: request, session,
environment, and backtrace (in that order).

Ȥ褦ˡ
  1. ꥯȾ
  2. å
  3. Ķѿ
  4. 㳰Υå
4Ĥ褦Ǥ

 app/views/exception_notifier  ȼ˥ӥ塼֤ȤǥޥǽȤΤȤǤ

ޤ404顼ξϡ᡼ΤϤʤ褦Ǥ
404Ǥ᡼줿ϤʤǤ

ޤʤ顢ȼ㳰ϥɥ뤷Ƥ᡼ΤϤޤ
ưǥ᡼ϡʴ notify_about_exception ƤӽФOKΤ褦Ǥ

  def index
    #risky operation here
  rescue StandardError => error
    #custom error handling here
    notify_about_exception(error)
  end


̷ǤäƤRailsϡƥफ᡼ФȼΤǤʤǡäǤϻȤ͡Ƥ᡼Ф餤ĤäƴʤǤ

JrailsƱActiveScaffoldѤ

RailsPrototype.jsjqueryȤΥץ饰ȤJrailsȤΤꡢʤΤǤActiveScaffoldȤޤʥץ饰Ʊ˻Ѥ褦ȤȡActiveScaffoldŬѤڡɽκݤˡPrototype.jsɤ߹ޤƤʤǤ衼ȤäܤƤޤޤ
ActiveScaffoldScaffoldAjaxǤߤʤΤǤܤ
http://activescaffold.com/

ºJrailsȤäƤΤ
<%= javascript_include_tag :defaults %>


äƤäƤPrototype.jsǤϤʤjQuery󥯥롼ɤΥޤ

ǡɤΤʡ Ĵ٤Ȥ
ActiveScaffoldWikiˤޤ

JrailsŬѤʤȤPrototypeͥˤȥǡ
  ActionView::Helpers::PrototypeHelper::JQUERY_VAR = 'jQuery'

Τ褦ˤơJavaScript$jQueryǾ񤭤ʤͤˤƤޤ


ºݤJrailsΥ򸫤ȡ

unless const_defined? :JQUERY_VAR
  JQUERY_VAR = '$'
end

ȤäҤޤ

ǤΤǡ̤jQueryȤʸǤʤƤɤ櫓ǡjQueryΥȤˤ⤢褦
var $j = jQuery.noConflict();
$j ȤǤʤ


ξ硢ActiveScaffoldAdminϤΥڡǤȤʤΤǡAdminѤδ쥳ȥ饯饹äơ¾AdminϥȥϤĤѾ褦ˤƤ뤿ᡢʴбƤOKǤ
class Admin::AdminControllerBase < ApplicationController
  ActionView::Helpers::PrototypeHelper::JQUERY_VAR = 'jQuery'
  layout 'admin'
  before_filter :block_none_admin
  ActiveScaffold.set_defaults do |config|
    config.ignore_columns.add [:created_at, :updated_at]
  end
end

adminȤlayoutȤäƤΤǡĤ⤳ʴ jQuery.noConflict ƤäPrototypeɤ߹ǤOKǤ
effects ץȤꤹΤ˺ʤ褦ˤޤ礦

<%= javascript_include_tag :defaults %>
		
<%= javascript_include_tag "prototype", "effects"  %> 
<%= active_scaffold_includes %>

Ǥڡ󥰤ǥ顼ˤʤ롢 ⤦äĴ٤ʤܤ
ץե

Ȥ

C#ɻŻRubyФʳȯԡ
ǶDockerȤؿȤAngularJS˥ϥޤäƤ롣
¢ȤĮȤȤ˽ס

QR
QR
  • 饤֥ɥ֥