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.

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.

また 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.

Continue reading Proof: Infinite Decimal of Rational Number is Recurring Decimal

Perl: 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.

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.