2013-03-25 72 views
0

我在ruby中完全是新手,但我想出瞭如何創建數據庫。 我這樣做Ruby On Rails從表格中獲得

rails generate scaffold MYTABLENAME 

,並在db /遷移我編輯的文件,以便它有我的專欄等

class CreateUsers < ActiveRecord::Migration 
    def change 
    create_table :users do |t| 
     t.string :username 
     t.string :password 
     t.string :email 
     t.timestamps 
    end 
    end 
end 

這是正確的嗎?

現在,我得到這樣的輸出:

invoke active_record 
    create db/migrate/20130325134909_create_posts.rb 
    create app/models/post.rb 
    invoke test_unit 
    create  test/unit/post_test.rb 
    create  test/fixtures/posts.yml 
    invoke resource_route 
    route resources :posts 
    invoke scaffold_controller 
    create app/controllers/posts_controller.rb 
    invoke erb 
    create  app/views/posts 
    create  app/views/posts/index.html.erb 
    create  app/views/posts/edit.html.erb 
    create  app/views/posts/show.html.erb 
    create  app/views/posts/new.html.erb 
    create  app/views/posts/_form.html.erb 
    invoke test_unit 
    create  test/functional/posts_controller_test.rb 
    invoke helper 
    create  app/helpers/posts_helper.rb 
    invoke  test_unit 
    create  test/unit/helpers/posts_helper_test.rb 
    invoke assets 
    invoke coffee 
    create  app/assets/javascripts/posts.js.coffee 
    invoke scss 
    create  app/assets/stylesheets/posts.css.scss 
    invoke scss 
    create app/assets/stylesheets/scaffolds.css.scss 
    invoke active_record 
    create db/migrate/20130325134909_create_posts.rb 
    create app/models/post.rb 
    invoke test_unit 
    create  test/unit/post_test.rb 
    create  test/fixtures/posts.yml 
    invoke resource_route 
    route resources :posts 
    invoke scaffold_controller 
    create app/controllers/posts_controller.rb 
    invoke erb 
    create  app/views/posts 
    create  app/views/posts/index.html.erb 
    create  app/views/posts/edit.html.erb 
    create  app/views/posts/show.html.erb 
    create  app/views/posts/new.html.erb 
    create  app/views/posts/_form.html.erb 
    invoke test_unit 
    create  test/functional/posts_controller_test.rb 
    invoke helper 
    create  app/helpers/posts_helper.rb 
    invoke  test_unit 
    create  test/unit/helpers/posts_helper_test.rb 
    invoke assets 
    invoke coffee 
    create  app/assets/javascripts/posts.js.coffee 
    invoke scss 
    create  app/assets/stylesheets/posts.css.scss 
    invoke scss 
    create app/assets/stylesheets/scaffolds.css.scss 

它也很好聽。但爲什麼會自動插入

app/views/posts/index.html.erb 
    app/views/posts/edit.html.erb 
    app/views/posts/show.html.erb 
    app/views/posts/new.html.erb 
    app/views/posts/_form.html.erb 

等等?這是標準嗎?如果是這樣 - 爲什麼?請解釋。

而另一個問題是: 在我的家庭控制器(作爲標準../home/index)我試圖創建一個用戶到我新的新鮮的數據庫,但它告訴我,未初始化的常量HomeController :: Users - 爲什麼?

這裏是我的代碼:

class HomeController < ApplicationController 
    def index 
     #@users = User.all 
     o = Users.new 
     o.username = "Jesper" 
     o.save 
    end 
end 
+2

嗯,你可能要檢查出許多偉大的初學者教程軌之一。 http://railsforzombies.org/ http://ruby.railstutorial.org/ruby-on-rails-tutorial-book – mhutter 2013-03-25 14:08:08

+2

你的代碼有很多問題。首先:你還沒有創建數據庫。您爲您的應用程序創建了所謂的腳手架,但該數據庫尚未創建。你的控制器的問題是你應該使用'User'而不是'Users'。請遵循初學者教程。如果您遇到這種情況,請使用Google。仍然卡住?在這裏提出具體問題。 – Mischa 2013-03-25 14:11:51

+0

http://guides.rubyonrails.org/getting_started.html#getting-up-and-running-quickly-with-scaffolding會回答你的第一個問題。你可以在同一個網站上找到第二個問題的答案:) – siekfried 2013-03-25 14:12:22

回答

1

如果你只是想生成你應該做一個模型:

rails generate model MYTABLENAME username:string password:string email:string 

腳手架產生了從模型到視圖(默認CRUD操作)。您應該閱讀http://guides.rubyonrails.org/getting_started.html。本指南將涵蓋所有基本知識。

-1

型號名稱必須是單數

class HomeController < ApplicationController 

    def index 
     #@users = User.all 
     o = User.new 
     o.username = "Jesper" 
     o.save 
    end 
end 

用戶,而不是用戶

0

我想你應該檢查一些視頻教程,瞭解如何一切正常。怎麼把沒有一點想用出會心軌

簡單google search的基本流程來修補你的知識,會做的伎倆