Ruby on Rails
提供: sha.ngri.la
使えるようにするまでは、Mountain LionでRuby On Railsを使えるようにするまでのとおり。いまのところ、不具合なく動いています。
目次
インストール
インストール
Rails アプリケーションを作る
- アプリケーションを作る
$ rails new [アプリケーション名] -d mysql
- scaffoldを生成
$ rails g scaffold [アプリケーション名] [カラム名1]:[メソッド名1] [カラム名2]:[メソッド名2] .....
例
$ rails g scaffold interested id:integer url:string title:string tag:string lastmodified:datetime
- config/database.ymlを編集
- migrateを実行
$ rake db:migrate
- config/routes.rbを編集。上記のrails g scaffoldの例なら、
InterestedIn::Application.routes.draw do resources :interesteds end
を
InterestedIn::Application.routes.draw do resources :interesteds root :to =>"interesteds#index" end
と変更する。
- Railsコマンドでgenerateしたのを取り消す。上記でgenerateした場合は、
$ rails destroy scaffold interested
リンクを作成する
link_to("表示する文字",url)
例えば、モデル名がinterestedでデータベースのカラム名がtitle、urlの場合(この認識で正しいのか?)、
link_to( interested.title ,interested.url)
3番目の引数で<a>タグの属性も指定できる。
link_to( interested.title ,interested.url,{:target=>"_blank"})
Twitter Bootstrapを使う
- Gemfileに追加
gem "twitter-bootstrap-rails", :group => :assets
- Terminal
$ bundle install (略) $ rails g bootstrap:install (略) $ rails g bootstrap:layout [LAYOUT_NAME] [*fixed or fluid]
例 fixedは1行、fluidは2行のことだと思う、、、
$ rails g bootstrap:layout application fixed
$ rails g bootstrap:themed [RESOURCE_NAME]
例
$ rails g bootstrap:themed interesteds
Link
- Twitter Bootstrap
- seyhunak/twitter-bootstrap-rails · GitHub
- Twitter bootstrap on rails
- 続『5分でRails3アプリ』をやってみた - 『Twitter Bootstrap』で見た目をサクッと変えてみた - 牌語備忘録 - pygo
kaminariを使う
pagenationを使えるようになります。
- #254 Pagination with Kaminari - RailsCasts
- kaminari + bootstrap によるデザイン崩れに対応する - akiinyoオフィシャルWeb日記
- gabetax/twitter-bootstrap-kaminari-views · GitHub
表示を整える
- 属性セレクタで、スタイルシートを柔軟に! [ホームページ作成 All About]
deviseを使ってユーザ認証機能をつける
- Gemfileを編集
gem "devise"
- shell
$ bundle install
- shell
$ rails g devise:install (略) =============================================================================== Some setup you must do manually if you haven't yet: 1. Ensure you have defined default url options in your environments files. Here is an example of default_url_options appropriate for a development environment in config/environments/development.rb: config.action_mailer.default_url_options = { :host => 'localhost:3000' } In production, :host should be set to the actual host of your application. 2. Ensure you have defined root_url to *something* in your config/routes.rb. For example: root :to => "home#index" 3. Ensure you have flash messages in app/views/layouts/application.html.erb. For example: <p class="notice"><%= notice %></p> <p class="alert"><%= alert %></p> 4. If you are deploying Rails 3.1 on Heroku, you may want to set: config.assets.initialize_on_precompile = false On config/application.rb forcing your application to not access the DB or load models when precompiling your assets. ===============================================================================
- shell
$ rails g devise user
- shell
$ rake db:migrate
この辺りでサーバをリスタート(ctrl-c,rails s)しないと、アクセスしてもエラーになる。
- app/controllers/****.controller.rb(****は、認証したいコントローラー)に berfore_filter :authenticate_user! を追加する。
class ****Controller < ApplicationController before_filter :authenticate_user!
- RailsApps/rails3-bootstrap-devise-cancan · GitHub
- Railsの第4世代認証エンジンDeviseのREADMEを翻訳してみた - LazyLoadLife
- Rails3アプリにDeviseで簡単に認証システムを組み込む - hrendohの日記
- Ruby on Rails ユーザー認証エンジン「Devise」の参考記事まとめ | CodeNote.net