Tag Archives: Rails

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 として 他のところで使えないので、 上に書いたやり方の方がきれいに見えます。


Drive Rails on Nginx and Passenger, Install Instructions


I setup rails on Passenger and Nginx. But I didn’t install from package, but compiled the source to hide some http response header. I wrote the instruction to install them, and what I did.

Environment

  • Ruby 2.2.2
  • Nginx 1.8
  • Passenger 5.0.18
  • EC2 Amazon Linux 2015.03

Overview

I suppose you installed ruby.

  1. Install Passenger
  2. Install Nginx
    • Download Nginx and required module
    • Compile and Install
  3. nginx configuration
  4. (Re)Boot nginx

Install Passenger

Install Passenger, as gem install passenger.

Install Nginx

If you execute passenger-install-nginx-module as root, nginx will be installed. But in the case, Nginx version and Passenger version should be presented in http response header.

For removing such no use headers, you should compile the source of nginx with nginx module.

Download Nginx and required module

Execute sudo su -, and switch user to root. And execute cd /usr/local/src.

Download Nginx

Download Nginx (tar.gz format). I downloaded Nginx 1.8.0, which was the latest stable nginx. You can find it at Nginx Official Download Page.

And extract it.

Download headers-more-nginx-module

Plain Nginx and Passenger respond http header including versions of Nginx and Passenger, and execution time, etc. Not to expose server’s internal information, use headers-more-nginx-module with Nginx.

I got the module (.tar.gz format) of latest version from github, which is on github.

Release list page : Releases · openresty/headers-more-nginx-module

After downloading, Extract it.

After extraction, change the .tar.gz file name to understandable name. Tab helps you to change the name, using extracted directory name.

Compile and Install

The installer require the following components. If there’s any deficit, installation will be stopped, so don’t worry.

  • C compiler (/usr/bin/cc)
  • C++ compiler (/usr/bin/c++)
  • wget or curl
  • curl development header with ssl support
  • Openssl development header
  • Zlib development header
  • rake, rack, ruby development header, rubygems

If you have installed nginx into the same place in the same way, the name old nginx binary will be changed to nginx.old.

Swap

If the RAM is less than 1 GB, like EC2 micro instance, swap memory is required. Execute the following commands as root.

Don’t worry, even if you start installation with small amount of memory, instruction is shown in installation process.

Execute passenger-install-nginx-module, with no option.

Now, the installation will start.

まずは 処理の説明が表示され、 Press Enter to continue, or Ctrl-C to abort. と表示されます。 Enter を押して続行します。 途中で()めたくなったら いつでも Ctrl-C を押せば止められます。

続いて Which languages are you interested in? と表示されるので 必要なものを画面の指示にしたがって選択し、 Enter を押します。

インストールされているソフトウェアのチェックが終わると、Do you want this installer to download, compile and install Nginx for you? と、 インストール方法を聞かれます。 ソースからコンパイルを行うので 2. No: I want to customize my Nginx installation. (for advanced users) を選びます。

And in my case, not installed component PCRE was automatically downloaded and installed.

Next, you should input source file location and compile option. I inputted the following.

Where is your Nginx source code located?
/usr/local/src/nginx-1.8.0
Where do you want to install Nginx to?
Pressed Enter, without any character.
Extra Nginx configure options
--add-module=/usr/local/src/headers-more-nginx-module-0.261
To use headers-more-nginx-module, add --add-module. Options set as default is printed on console.

At last, confirmation message will be shown, then Enter if there is no mistake. And compiling and installing will be started.

nginx configuration

I copied /opt/nginx/conf/nginx.conf.default to /opt/nginx/conf/nginx.conf.

For running Rails application, the following configuration helps. I show a part of the configuration. I set client_max_body_size 10 MB for uploading images. (It’s 1M as default.) Other configuration setting is explained on Phusion Passenger users guide, Nginx version or so.

And add code to modify response header.

Check the configuration file with the following command. It’s ok if no error is shown.

(Re)Boot Nginx

I usually use /opt/nginx/sbin/nginx -s reload for rails reloading, but it wasn’t effective in this case.

Stop Nginx with the command /opt/nginx/sbin/nginx -s stop, and boot it by execution of /opt/nginx/sbin/nginx.

If you launch nginx first time, only execute /opt/nginx/sbin/nginx.


Rails 4 : Load Seed from TSV


Some days ago, I was requested to enable to load seed data from excel, by my client, in rails 4 project. I told that data to be edited should be managed in database, not excel. But he really requested excel, then I and he decided load data from tsv. Excel is functional, so TSV is appropriate.

Select data area and copy and paste with mouse, Excel data can be pasted as TSV data to text editor, and vise versa.

Environment

  • Ubuntu 14.04 LTS
  • Rails 4.1.8
  • Ruby 2.2.2

Direction

  1. Create feature to load TSV data as a task, and enable to execute with rake.
  2. On executing rake db:seed, execute the task to load TSV.
Continue reading Rails 4 : Load Seed from TSV

Simple Rails Log Rotation


Here’s the way of simple log rotation in Rails.

Environment

  • Rails 4.1.8
  • Ruby 2.2.2
  • Ubuntu 15.04 (64bit)

Setting

First, add the following code to config/application.rb.

In the above, log file will be split by 100 MB (104,857,600 bytes) and limit 10 files. Namely, it will save about 1 GB logs. また、 10 ファイル を超えると、古いものから削除されていきます。

Rails can configure logger as daily, weekly, etc., but in that case old log will not be deleted.

Test

Before testing in development environment, create log file with the following command.

bs represents block size, count represents times to write.

And launch server with rails s, access some pages. Then, development.log and development.log.0 will be created.

About Rails logging, you can use the other ways like SyslogLogger and Linux logrotate, for example.


The Way I Used Magnum CI for Rails Test


Magnum CI is a CI environment which we can use for free. Here, I wrote how I setup Rails code into Magnum CI, which is managed at Bitbucket.

Continue reading The Way I Used Magnum CI for Rails Test