LoginSignup
9
9

More than 5 years have passed since last update.

それっぽいRailsアプリ開発

Last updated at Posted at 2013-05-08

http://sqale.jp/
ユーザー登録。
sshを登録。
新しいアプリケーションを作成。

プロジェクトを作ってscaffold。

rails new sqale-sample -T
cd sqale-sample/
rails g scaffold topic name:string mail:string title:string body:text password:string
rake db:migrate

日本語用に設定変更。

config/application.rb
require File.expand_path('../boot', __FILE__)

# Pick the frameworks you want:
require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "sprockets/railtie"
# require "rails/test_unit/railtie"

if defined?(Bundler)
  # If you precompile assets before deploying to production, use this line
  Bundler.require(*Rails.groups(:assets => %w(development test)))
  # If you want your assets lazily compiled in production, use this line
  # Bundler.require(:default, :assets, Rails.env)
end

module SqaleSample
  class Application < Rails::Application
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.

    # Custom directories with classes and modules you want to be autoloadable.
    # config.autoload_paths += %W(#{config.root}/extras)

    # Only load the plugins named here, in the order given (default is alphabetical).
    # :all can be used as a placeholder for all plugins not explicitly named.
    # config.plugins = [ :exception_notification, :ssl_requirement, :all ]

    # Activate observers that should always be running.
    # config.active_record.observers = :cacher, :garbage_collector, :forum_observer

    # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
    # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
    # config.time_zone = 'Central Time (US & Canada)'
    config.time_zone = 'Tokyo'
    config.active_record.default_timezone = :local

    # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
    # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
    # config.i18n.default_locale = :de
    config.i18n.default_locale = :ja

    # Configure the default encoding used in templates for Ruby 1.9.
    config.encoding = "utf-8"

    # Configure sensitive parameters which will be filtered from the log file.
    config.filter_parameters += [:password]

    # Enable escaping HTML in JSON.
    config.active_support.escape_html_entities_in_json = true

    # Use SQL instead of Active Record's schema dumper when creating the database.
    # This is necessary if your schema can't be completely dumped by the schema dumper,
    # like if you have constraints or database-specific column types
    # config.active_record.schema_format = :sql

    # Enforce whitelist mode for mass assignment.
    # This will create an empty whitelist of attributes available for mass-assignment for all models
    # in your app. As such, your models will need to explicitly whitelist or blacklist accessible
    # parameters by using an attr_accessible or attr_protected declaration.
    config.active_record.whitelist_attributes = true

    # Enable the asset pipeline
    config.assets.enabled = true

    # Version of your assets, change this if you want to expire all your assets
    config.assets.version = '1.0'
  end
end

I18nの日本語使うためにごにょごにょする。

Gemfile
source 'https://rubygems.org'

gem 'rails', '3.2.8'

# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'

gem 'sqlite3'
gem 'i18n_generators'

# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'

  # See https://github.com/sstephenson/execjs#readme for more supported runtimes
  # gem 'therubyracer', :platforms => :ruby

  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'

# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'

# To use Jbuilder templates for JSON
# gem 'jbuilder'

# Use unicorn as the app server
# gem 'unicorn'

# Deploy with Capistrano
# gem 'capistrano'

# To use debugger
# gem 'debugger'
rails g i18n ja

production環境ではmysqlを利用するようGemfileを更新。

Gemfile
gem 'rails', '3.2.8'

# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'

gem 'sqlite3'


# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'

  # See https://github.com/sstephenson/execjs#readme for more supported runtimes
  # gem 'therubyracer', :platform => :ruby

  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'

group :production do
  gem 'mysql2'
end

# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'

# To use Jbuilder templates for JSON
# gem 'jbuilder'

# Use unicorn as the app server
# gem 'unicorn'

# Deploy with Capistrano
# gem 'capistrano'

# To use debugger
# gem 'ruby-debug19', :require => 'ruby-debug'

sqaleからデータベース接続情報をコピーしてdatabase.ymlにペースト。

config/database.yml
# SQLite version 3.x
#   gem install sqlite3
#
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem 'sqlite3'
development:
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 5
  timeout: 5000

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  adapter: sqlite3
  database: db/test.sqlite3
  pool: 5
  timeout: 5000

production:
  adapter: mysql2
  encoding: utf8
  username: (sqaleでコピーした値)
  password: (sqaleでコピーした値)
  database: (sqaleでコピーした値)
  host: mysql001.sqale.jp

ソースをcommitして、sqaleのgitリポジトリをremoteに追加。

git init
git add .
git commit -m "first commit."
git remote add sqale (sqaleに書かれているgit sshの値)

ソースをsqaleにpush。

git push sqale master

ブラウザでSqale>アプリケーションの操作>マイグレーションの実行。

Qiita

Qiita

twitter bootstrapを適用していく。
まずGemfileに必要なGemを追記。
今回追加したのは

  • simple_form
  • twitter-bootstrap-rails とそれに付随する必要なgem

変更後のGemfileは以下

Gemfile
source 'https://rubygems.org'

gem 'rails', '3.2.8'

# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'

gem 'sqlite3'
gem 'i18n_generators'
gem 'sqlite3'
gem 'simple_form', '~> 2.0.4'

# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'

  # See https://github.com/sstephenson/execjs#readme for more supported runtimes
  # gem 'therubyracer', :platforms => :ruby
  gem 'therubyracer', :platforms => :ruby
  gem 'less-rails'
  gem 'twitter-bootstrap-rails', :git => 'http://github.com/seyhunak/twitter-bootstrap-rails.git'
  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'

group :production do
  gem 'mysql2'
end

# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'

# To use Jbuilder templates for JSON
# gem 'jbuilder'

# Use unicorn as the app server
# gem 'unicorn'

# Deploy with Capistrano
# gem 'capistrano'

# To use debugger
# gem 'debugger'

twitter-bootstraprails simple-formを適用。

bundle install

rails g bootstrap:install
rails g simple_form:install --bootstrap
rails g bootstrap:layout application fluid -y
rails g bootstrap:themed Topics -y

いい感じのテーマを http://bootswatch.com/ から適当に
variables.lessをダウンロードし、中身をbootstrap_and_overrides.css.lessにコピペ。

app/assets/stylesheets/scaffolds.css.scssを全てコメントアウト
(※ファイルごと消せばいいんだけど、scaffoldしたときに勝手に作られるからさ)

showの画面で値がnilの場合

が連れるので
値がnilの場合スペースを出力するヘルパーを作成。
app/helpers/application_helper.rb
module ApplicationHelper

  def nil_space(val)
    if val.blank?
      return raw("&nbsp;")
    end
    val
  end

end
app/views/topics/show.html.erb
<%- model_class = Topic -%>
<div class="page-header">
  <h1><%=t '.title', :default => model_class.model_name.human %></h1>
</div>

<dl class="dl-horizontal">
  <dt><strong><%= model_class.human_attribute_name(:name) %>:</strong></dt>
  <dd><%= nil_space @topic.name %></dd>
  <dt><strong><%= model_class.human_attribute_name(:mail) %>:</strong></dt>
  <dd><%= nil_space @topic.mail %></dd>
  <dt><strong><%= model_class.human_attribute_name(:title) %>:</strong></dt>
  <dd><%= nil_space @topic.title %></dd>
  <dt><strong><%= model_class.human_attribute_name(:body) %>:</strong></dt>
  <dd><%= nil_space @topic.body %></dd>
  <dt><strong><%= model_class.human_attribute_name(:password) %>:</strong></dt>
  <dd><%= nil_space @topic.password %></dd>
</dl>

<div class="form-actions">
  <%= link_to t('.back', :default => t("helpers.links.back")),
              topics_path, :class => 'btn'  %>
  <%= link_to t('.edit', :default => t("helpers.links.edit")),
              edit_topic_path(@topic), :class => 'btn' %>
  <%= link_to t('.destroy', :default => t("helpers.links.destroy")),
              topic_path(@topic),
              :method => 'delete',
              :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) },
              :class => 'btn btn-danger' %>
</div>

deployしたら動かなかったのでapplication_helperを編集。

app/helpers/application_helper.rb
module ApplicationHelper

  def nil_space(val)
    if val.blank?
      return raw("&nbsp;")
    end
    val
  end

  def bootstrap_flash
    flash_messages = []
    flash.each do |type, message|
      # Skip Devise :timeout and :timedout flags
      next if type == :timeout
      next if type == :timedout
      type = :success if type == :notice
      type = :error if type == :alert
      text = content_tag(:div,
                         content_tag(:button, raw("&times;"), :class => "close", "data-dismiss" => "alert") +
                             message, :class => "alert fade in alert-#{type}")
      flash_messages << text if message
    end
    flash_messages.join("\n").html_safe
  end
end

9
9
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
9