Sorry, this entry is only available in 日本語.
All posts by Kenji
Use AngularJS on Rails form_for
I used AngularJS on Rails form_for. The most annoying thing is form_for generates form tag with action attribute.
Environment
- Rails 4.1.8
- Ruby 2.2.2
- AngularJS 1.4.7
- Ubuntu 15.04
Circumstance
Rails form_for generates form tag with some attributes, including action. Of course, we can add other attributes. But, AngularJS conflicts with this.
In the following code, problem occurs.
|
1 2 3 4 5 |
<%=form_for(@user, html: {'data-ng-submit': 'mainCtrl.create()', novalidate: :novalidate, name: 'createForm'}) do |f|%> <%=f.label :name%> <%=f.text_field :name, required: :required, 'data-ng-model': 'user.name'%> <button>submit</button> <%end%> |
After clicking the button, AngularJS process will be executed and original form process will be executed.
AngularJS prevent original process when form doesn’t have action attribute, but rails add action attributes automatically. I couldn’t find the way to prevent rails from adding action attribute.
Solution
Add onsubmit="return false;" to the form tag.
|
1 2 3 4 5 |
<%=form_for(@user, html: {'data-ng-submit': 'mainCtrl.create()', novalidate: :novalidate, name: 'createForm', onsubmit: "return false;"}) do |f|%> <%=f.label :name%> <%=f.text_field :name, required: :required, 'data-ng-model': 'user.name'%> <button>submit</button> <%end%> |
また AngularJS: How to prevent form submission after click on button? に記載されているような方法もありますが、 javascript として 他のところで使えないので、 上に書いたやり方の方がきれいに見えます。
Proof: Infinite Decimal of Rational Number is Recurring Decimal
I prove that infinite decimal of rational number is recurring decimal. It is decimal of rational number but is not finite decimal.
While the process of division may seem intuitive, I will provide a more concrete explanation as a formal proof.
Feeling
For example, when 89 is sequentially divided by 13, including decimal places, the remainders are as follows:
\begin{eqnarray*} 89 \div 13 & = & 6 \; \textrm{remainder is} 11 \\ 110 \div 13 & = & 8 \; \textrm{remainder is} 6 \\ 60 \div 13 & = & 4 \; \textrm{remainder is} 8 \\ 80 \div 13 & = & 6 \; \textrm{remainder is} 2 \\ 20 \div 13 & = & 1 \; \textrm{remainder is} 7 \\ 70 \div 13 & = & 6 \; \textrm{remainder is} 5 \\ 50 \div 13 & = & 3 \; \textrm{remainder is} 11 \\ 110 \div 13 & = & 8 \; \textrm{remainder is} 6 \end{eqnarray*}On the 7th division, the same remainder as in the 1st division appeared again. Since there are only 12 possible remainers when divided by 13, ranging from 1 to 12, if the division doesn’t result in a perfect quotient after 13 divisions, it becomes evident that at some point, the same remainder would recur, leading to a repeating decimal.
Proof: \( \sqrt{2} \) is an irrational number
Let’s assume the definition of rational numbers is known. Real numbers that are not rational are called irrational numbers.
Here, we will prove that \( \sqrt{2} \) is an irrational number.
Continue reading Proof: \( \sqrt{2} \) is an irrational numberPerl: The way to Send Mail
I’ll introduce the way to send mail in Perl. 私はこの仕組みを、サーバの状態を必要な時にメールで通知するスクリプトなどで利用しています。 (ref.: Server Monitoring Script made by me, a Perl beginner)
Environment
Computer which runs perl
I tried in 2 environments.
Case 1
- Ubuntu 15.04
- Perl 5.20.2
Case 2
- EC2 Amazon Linux 2015.03
- Perl 5.16.3
SMTP Server
- EC2 Amazon Linux 2015.03
- Postfix
- Authenticate with password in CRAM-MD5 way
Code
Here’s the most simple sample code.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#!/usr/bin/perl use strict; use utf8; use warnings; use Digest::MD5; use Authen::SASL; use Net::SMTP; my $header = "From: from@sample.com"; $header .= "nTo: to@sample.com"; $header .= "nSubject: subject"; $header .= "nMIME-Version: 1.0"; $header .= "nContent-Type: text/plain; charset=UTF-8"; $header .= "nContent-Transfer-Encoding: Base64n"; $smtp = Net::SMTP->new('mail.sample.com', Port => 587); $smtp->auth('user@sample.com', 'password'); $smtp->mail('from@sample.com'); $smtp->to('to@sample.com'); $smtp->data(); $smtp->datasend($header); $smtp->datasend("n"); $smtp->datasend('message body'); $smtp->quit; |
If you don’t require password authentication, there’s no need to use Digest::MD5 or Authen::SASL, or the line written auth. But supposing CRAM-MD5 authentication, they are necessary. auth method of Net::SMTP requires Authen::SASL, which requires Digest::MD5.
Authen::SASL が入ってないと auth を実行しても認証がおこなわれません。 Digest::MD5 が入っていないと、 Error: No SASL mechanism found
と表示されます。
パスワードの認証方式は 自動で判別されています。 認証サーバで Digest-MD5 が有効であれば CRAM-MD5 よりも Digest-MD5 を優先的に使います。
そして、認証をするにはインストールしなければならないパッケージがあります。
- For Ubuntu
- libdigest-hmac-perl
- libauthen-sasl-perl
- For Amazon Linux
- perl-Authen-SASL
Amazon Linux の場合、 MD5 のモジュールが入っているパッケージ perl-Digest-MD5 はあらかじめインストールされています。
In many case, perl is installed in advance and Net::SMTP module is available to use, but authentication requires additional module installation.