Ruby on Rails

提供: sha.ngri.la
2012年9月12日 (水) 09:44時点におけるTara.ttm (トーク | 投稿記録)による版 (deviseを使ってユーザ認証機能をつける)
移動先: 案内検索

使えるようにするまでは、Mountain LionでRuby On Railsを使えるようにするまでのとおり。いまのところ、不具合なく動いています。

インストール

Ruby on RailsをVPSで動かす

インストール

Ruby on RailsをVPSで動かす

Rails アプリケーションを作る

  1. アプリケーションを作る
    $ rails new [アプリケーション名] -d mysql
    
  2. scaffoldを生成
    $ rails g scaffold [アプリケーション名] [カラム名1]:[メソッド名1] [カラム名2]:[メソッド名2] .....
    

    $ rails g scaffold interested id:integer url:string title:string tag:string lastmodified:datetime
    
  3. config/database.ymlを編集
  4. migrateを実行
    $ rake db:migrate
    
  5. 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
    

    と変更する。

  6. 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を使う

  1. Gemfileに追加
    gem "twitter-bootstrap-rails", :group => :assets
  2. Terminal
  3. $ 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

kaminariを使う

pagenationを使えるようになります。

表示を整える

deviseを使ってユーザ認証機能をつける

  1. Gemfileを編集
    gem "devise"
  2. shell
    $ bundle install
  3. 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.
    
    ===============================================================================
    
  4. shell
    $ rails g devise user
  5. shell
    $ rake db:migrate

    この辺りでサーバをリスタート(ctrl-c,rails s)しないと、アクセスしてもエラーになる。

  6. app/controllers/****.controller.rb(****は、認証したいコントローラー)に berfore_filter :authenticate_user! を追加する。
    class ****Controller < ApplicationController
      before_filter :authenticate_user!
    

Link