Ȥ Blog

Ruby,C#

2011ǯ02

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

Rails3jQueryΥ饤ȥɤΥץȤȤƻȤ

ŻǤϤޤRails3ϤäȤʤΤǤʻäƤȤĤޤǤäƤ⿨뵡ʤʤΤǡäȤJavaScriptʤ󤫤ѤΥץȤRails3ǰĺäƤȻפäƤߤޤ
ȤꤢRails3򥤥󥹥ȡ롣
gem install rails
礳äȤJavaScriptȤѤΥץȤʤΤǡ js_spikeä̾ǥץȤäƤߤ롣
rails new js_spike
public/javascriptsƤߤȥǥեȤǤˤäPrototypeˤʤäƤ褦Ǥ
-rw-rw-r-- 1 satoyan satoyan    148 Feb 28 15:52 application.js
-rw-rw-r-- 1 satoyan satoyan  34787 Feb 28 15:52 controls.js
-rw-rw-r-- 1 satoyan satoyan  31056 Feb 28 15:52 dragdrop.js
-rw-rw-r-- 1 satoyan satoyan  38467 Feb 28 15:52 effects.js
-rw-rw-r-- 1 satoyan satoyan 162353 Feb 28 15:52 prototype.js
-rw-rw-r-- 1 satoyan satoyan   6278 Feb 28 15:52 rails.js
jquery-railsȤgemȥ饤ȥɤΥץȤ򤹤٤jQueryΤΤ֤Ƥ褦ʤΤǤ򥤥󥹥ȡ뤷Ƥߤ롣 Gemfile ΰԤɲá
	gem 'jquery-rails'
bunldle install ¹
	#bundle install ¹
	
	Fetching source index for http://rubygems.org/
	Using rake (0.8.7) 
	Using abstract (1.0.0) 
	Using activesupport (3.0.5) 
	Using builder (2.1.2) 
	Using i18n (0.5.0) 
	Using activemodel (3.0.5) 
	Using erubis (2.6.6) 
	Using rack (1.2.1) 
	Using rack-mount (0.6.13) 
	Using rack-test (0.5.7) 
	Using tzinfo (0.3.24) 
	Using actionpack (3.0.5) 
	Using mime-types (1.16) 
	Using polyglot (0.3.1) 
	Using treetop (1.4.9) 
	Using mail (2.2.15) 
	Using actionmailer (3.0.5) 
	Using arel (2.0.9) 
	Using activerecord (3.0.5) 
	Using activeresource (3.0.5) 
	Using bundler (1.0.10) 
	Using thor (0.14.6) 
	Using railties (3.0.5) 
	Using rails (3.0.5) 
	Installing jquery-rails (0.2.7)  #󥹥ȡ뤵ޤ...
	Using sqlite3 (1.3.3) 
"rails g" generator򸫤ƤߤȿjQuery󤬤ǤƤޤ
	#rails g generatorΥإפ򸫤Ƥߤ
	
	Usage: rails generate GENERATOR [args] [options]

	General options:
	  -h, [--help]     # Print generator's options and usage
	  -p, [--pretend]  # Run but do not make any changes
	  -f, [--force]    # Overwrite files that already exist
	  -s, [--skip]     # Skip files that already exist
	  -q, [--quiet]    # Suppress status output

	Please choose a generator below.

	Rails:
	  controller
	  generator
	  helper
	  integration_test
	  mailer
	  migration
	  model
	  observer
	  performance_test
	  plugin
	  resource
	  scaffold
	  scaffold_controller
	  session_migration
	  stylesheets

	Jquery:
	  jquery:install
	
Ȥ jquery:install ɽƤߤ롣
	#rails g jquery:install --help
	
	Usage:
	  rails generate jquery:install [options]

	Options:
	  [--version=VERSION]  # Which version of jQuery to fetch
	                       # Default: 1.5
	  [--ui]               # Include jQueryUI

	Runtime options:
	  -s, [--skip]     # Skip files that already exist
	  -p, [--pretend]  # Run but do not make any changes
	  -q, [--quiet]    # Supress status output
	  -f, [--force]    # Overwrite files that already exist

	This generator downloads and installs jQuery, jQuery-ujs HEAD, and (optionally) the newest jQuery UI
	
СλjQueruyUIȤɤλ꤬Ǥ餷Τǡä¹ԤƤߤ롣
	#rails g jquery:install --ui --force
	
    remove  public/javascripts/controls.js
    remove  public/javascripts/dragdrop.js
    remove  public/javascripts/effects.js
    remove  public/javascripts/prototype.js
  fetching  jQuery (1.5)
    create  public/javascripts/jquery.js
    create  public/javascripts/jquery.min.js
  fetching  jQuery UI (latest 1.x release)
    create  public/javascripts/jquery-ui.js
    create  public/javascripts/jquery-ui.min.js
  fetching  jQuery UJS adapter (github HEAD)
     force  public/javascripts/rails.js
	
public/javascriptsƤߤȡjqueryϢΥե뤬Ƥޤ
  # ll public/javascripts
	-rw-rw-r-- 1 satoyan satoyan    148 Feb 28 16:25 application.js
	-rw-rw-r-- 1 satoyan satoyan 216840 Feb 28 16:40 jquery.js
	-rw-rw-r-- 1 satoyan satoyan  85260 Feb 28 16:40 jquery.min.js
	-rw-rw-r-- 1 satoyan satoyan 367486 Feb 28 16:40 jquery-ui.js
	-rw-rw-r-- 1 satoyan satoyan 198792 Feb 28 16:40 jquery-ui.min.js
	-rw-rw-r-- 1 satoyan satoyan   5033 Feb 28 16:40 rails.js
Ǥpublic/stylesheetsˤjQueryUIѤCSSϤǤƤʤ褦Ǥ ʤᤸ󡢤ȸȤCSSGoogleCDNѤ뤳Ȥˤޤ Ȥꤨ redmondȤơޤΤΤȤ褦applicataion.html.erb˽񤭤Ǥ
	  <%= stylesheet_link_tag :all %>
	  <%= javascript_include_tag :defaults %>
	  <%= csrf_meta_tag %>
		<%= stylesheet_link_tag "https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.10/themes/redmond/jquery-ui.css" %>
ȤꤢʴjQueryϤǤޤCSSäƤʤؤʤ褦ʵɤʡ
ȤΤʤ

ϥɥ뤵ʤä㳰᡼ΤƤץ饰 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 %>

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

MonoDevelopMacǡˤǥץȥ顼

MonoDevelop򥢥åץǡȤơץ¹Ԥ褦comand + R 򲡤顢ʥ顼Ф
01


̤˥ӥɤƥߥʥ뤫
mono ¹ԥե 
äƤʤưΤǡñIDEʤΤȻפȤƤ顢ɤ ץȤΥץ

Run on external console 

̵ˤФ餷
23


Υץ󤬤Ĥ餢äΤΤʤʤǥեȤͭˤʤäƤΤϤ狼ޤ󤬡Ȥˤкޤ̤command + R ¨¹Ԥ뤳ȤǤ롣
⤽⳰󥽡äƤǤϲؤƤ
IDEΤΤǤϤʤߥʥȤȤäƤȤʤΤʡ 褯狼ޤ


MonoDevelopapp.config

.netץΥץꥱեϥǥեȤǤ
¹ԥե̾.config 
Ȥ̾ˤʤΤǤ̾VisualStudioǤApp.configȤեԽơӥɤȤϽ˥͡वơhoge.exe.config ߤʴˤƤޤ

ǡMonoDevelop(ޥåǡˤǤϤɤ

եοˤäݤΤޤ

30

Ȥꤢ̾App.config ˤŬ򤫤ƥӥɤƤߤޤˤϼ¹ԥե뤷줺

IDEǥեΥץѥƥ򸫤Ƥߤȡäݤȯ
55

BuildAction: ApplicationDefinition
Copty to output directory: Always copy | Copy if newer

ˤơ⤦٥ӥɤȡ
¹ԥե̾.config Ȥե뤬̵ɤ߹ޤ
 

mono C# gmail

XmlWebServiceʤäƺʤΤʡˤáƤη̤򸫤ʤΤ顼ʤΤӤ˥᡼ΤʤäΤǡŻǤRubyȤJavaФäʤΤǡC#ǺäƤߤޤ
cronưΤǡmonoC#Ǥ
static void sendMail (string message, string subject)
{
		var smtp = new SmtpClient ("smtp.gmail.com", 587);
		smtp.EnableSsl = true;

		//桼̾@gmail.comϴޤʤ
		smtp.Credentials = new NetworkCredential ("gmailAccount", "password");
		ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
		
		var msg = new MailMessage ();
		msg.Body = message;
		msg.Subject = subject;
		msg.To.Add ("hoge@hoge"); 
		msg.From = new MailAddress ("hoge2@hoge");
		smtp.Send (msg);

ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
줬ʤȡ
System.Net.Mail.SmtpException: Message could not be sent. ---> System.IO.IOException: The authentication or decryption has failed. ---> System.InvalidOperationException: SSL authentication error: RemoteCertificateNotAvailable, RemoteCertificateChainErrors
ܤƤޤޤ
ϥ顼ȤåǤ礦ɡȤꤢǤϡtrue֤ʤǤ衼ȤƤޤ
ˤĤƤϡ
http://www.atmarkit.co.jp/fdotnet/dotnettips/867sslavoidverify/sslavoidverify.html
ʬ䤹⤷Ƥޤ

Rails: fixture:loadno(ʥСΰաˤȤ̾

Rails

rake db:fixtures:load

¹Ԥ

PGError: ERROR: column "false" of relation xxxxx

⤽"false" ʤƤϤʤɡ

ǡFixtureȤǧƤߤ
noʥʥСȤ̣Ǥ̾ˤΤȻפޤˤȤ̾ȯ
fixutureϤʴ

id: 1
name: satoyan
no: 
job: 
....

Ĥ򤷤Ƥ褦Ǥ
ȤäƤDBPostgresqlʤΤǤbooleanΥФơno, 0, false, fȤƥ false ΰդˤʤ褦ǡɤRailsnoȤʸ̾ʤΤˤؤ餺falseȤʸ֤ƤƤޤä褦Ǥ

ȸȤ no 餿 "no" Ȥ뤳ȤDz衣
ǽ顢֤Ϥ  falseʤ󤢤櫓͡פȤäƤФ餯𤷤Ƥޤޤ


JavaǥեɤǤߤ POIäƤʤ

ǶJavaǥե򰷤ɬפФƤơʹȤˤPOIȤΤȤ餷
POI100Javaǽ񤫤줿ޥեʤǺ줿ɥȤ򰷤饤֥ǡPOIȤ̾ˤ2Ťΰ̣Ƥ뤽Ǥ
ܤϲΥȤǡ


ǡ®ǿǤ3.7ɡ


EclipseǿץȤơPOI饤֥ӥɥѥɲá
05

ץѤΥե
55

եXMLǤϤʤ켰ΡʤȤǤǤŪʵxls¸
POIΤξΥեޥåȤбƤ褦Ǥ
59

åɤ򻲹ͤ˥ɤ񤤤Ƥߤ롣
ʤľŪ˰褦Ǥ
    1 import java.io.FileInputStream;
2 import java.io.InputStream;
3 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
4 import org.apache.poi.ss.usermodel.Row;
5 import org.apache.poi.ss.usermodel.Sheet;
6 import org.apache.poi.ss.usermodel.Workbook;
7
8
9 public class Main {
10 public static void main(String[] args){
11 try {
12 InputStream file = new FileInputStream("data/hello_poi.xls");
13 Workbook book = new HSSFWorkbook(file);
14 Sheet sheet = book.getSheetAt(0);
15 for(int rowIndex = 0; rowIndex < 2; rowIndex++) {
16 Row row = sheet.getRow(rowIndex);
17 System.out.printf("%s, %s\n", row.getCell(0), row.getCell(1));
18 }
19 file.close();
20 } catch (Exception e) {
21 e.printStackTrace();
22 }
23
}

24 }

25

¹ԤȤɽǤޤ
18

Ͻ񤭽ФäƤߤ롣



ץե

Ȥ

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

QR
QR
  • 饤֥ɥ֥