זהו מדריך כללי על ההתקנה והעבודה עם רובי און ריילס
הדבר הראשון שיש לעשות, הוא להתקין את רובי ורובי און ריילס על מערכת ההפעלה שלך ולהתקין מסד נתונים. מסד הנתונים הרשמי של ריילס הוא SQLite, אך ניתן להשתמש במגוון רחב של מסדי נתונים אחרים. מדריכי התקנה מפורטים לרובי און ריילסלמקינטוש, לחלונות, ולינוקס.
- צור פרוייקט ריילס ע”י מתן הפקודה
$ rails myapp
בשורת הפקודה שלך. עבור לתיקיה החדשה שנוצרה ע”י הקשה של הפקודה
$ cd myapp
הפעל את שרת האפליקציה שלך
$ ruby script/server
Note: פקודה זו תריץ שרת אפליקציה המתאים לסביבות פיתוח בלבד
- פתח את הדפדפן וגש אל הכתובת
http://localhost:3000
ואם הכל הלך כשורה, תוכל לראות את דף ברירת המחדל של האפליקציה שלך.
פתח את האפליקציה שלך בעורך לבחירתך וגש לקובץ
config/database.yml
זהו הקובץ בו נשמרות ההגדרות של הגישה למסד הנתונים של האפליקציה, הגדרות כמו שם משתמש וסיסמא, שרת וסוג מסד הנתונים. בדרך כלל תדרש לעשות שינויים כדי להפנות את האפליקציה למסד הרלוונטי, במקרה של האפליקציה הזו, אין צורך בשינויים. שים לב ששם מסד הנתונים הדיפולטי נגזר משם האפליקציה שבחרת קודם
פתח חלון שורת פקודה, גש לספרית הפרוייקט והקש את הפקודה
$ rake db:create
ריילס תיצור את מסד הנתונים שלך לסביבת הפיתוח, כפי שציינת בקובץ ההגדרות של מסד הנתונים
עכשיו ניצור את הטבלה הראשונה שלנו ואת הממשק הבסיסי לניהול הנתונים שלה, ממשק זה נקרא פיגום ומשמש למטרה הזו בלבד.
הערה: למרות שזה מאוד מפתה, פיגומים נועדו למטרות תחזוקה ובניה ראשונית ולא מומלץ להשתמש בהם מעבר לשימושים הנוגעים למהלך הפיתוח של האפליקציה.
כדי לראות איך משתמשים בפיגומים, נראה למחולל הפיגומים ע”י הפקודה
$ ruby script/generate scaffold
נניח שברצוננו ליצור טבלת משתמשים, כדי ליצור טבלת משתמשים בעזרת פיגום נקיש את הפקודה הבאה
$ ruby script/generate scaffold user first_name:string last_name:string active:boolean
פקודה זו מייצרת לנו קונטרולר לניהול משתמשים, מודל לייצוג משתמש ממסד הנתונים, את מבנה הטבלה במסד הנתונים ואת דפי ה-html הנחוצים לנו לשם ביצוע פעולות על רשומות משתמשים.
הערה: מחולל הפיגום לא יצר את הטבלה במסד הנתונים, אנו צריכים להקיש עוד פקודה בטרם הטבלה תיווצר במסד הנתונים
כדי ליצור את הטבלה נצטרך להריץ פקודת הגירה למסד הנתונים
$ rake db:migrate
עכשיו נוכל לגשת לדפדן ולהקיש את הכתובת הבאה כדי להתחיל לנהל את המשתמשים במערכת
http://localhost:3000/users
בממשק זה תוכל להוסיף משתמשים, לצפות באחד או בכולם, לערוך ואף למחוק משתמשים.
$ rake db:rollback
This will roll back one migration.
/db/migrations/ directory. It should be the only file.up method to migrate up, and down method to migrate down.t.boolean :active line, add: t.text :bio
rake db:migrate again to migrate your database up: $ rake db:migrate
/app/views/users/new.html.erb file. This is what renders our New User form. We need to add a bio field using the following code below the active checkbox.<p> <%= f.label :bio %><br /> <%= f.text_area :bio %> </p>
For extra credit, you could use the same code if you wanted to update the edit view (in /app/views/users/edit.html.erb) so you can edit a user's bio./app/views/users/show.html.erb file, and underneath the Active field, adding:<p> <b>Bio:</b> <%=h @user.bio %> </p>
show page (and not the index). When you get to this part, let out a barbaric “woot!”, you've made some great progress./app/models/user.rb.class User < ActiveRecord::Base validates_presence_of :first_name, :last_name end
For extra credit, add another validation to your app just for fun.
scaffold command, a /app/views/layouts/users.html.erb file was created, and used as your layout.users.html.erb to application.html.erb. As a Rails convention, now that you have an application.html.erb file in your layouts directory, all controllers will use this by default (you can override this later if you want).application.html.erb and add something like: <h2>Rails Rocks!</h2>
$ ruby script/console
You are now in a Ruby IRB session which has access to your models.
$ ActiveRecord::Base.logger = Logger.new(STDOUT)
puts "Hello Dude!"
u = User.new u.first_name = "Gregg" u.last_name = "Pollack" u.save
Notice the SQL that is generated.
User.create(:first_name => "Joe", :last_name => "Blow")
If you hit any errors, check for syntax problems.
u = User.find_by_first_name("Joe") u.last_name = "Johnson" u.save
Notice the SQL that gets generated; nice!
u.destroy
User.first User.last User.all
Feel free to set a variable with the values, and play around with changing attributes. Do note that .all returns an array, so you may want to use [0] or [2] to refer to a particular item in the array.
User.all.each {|user| puts user.first_name.upcase }
User.all.map {|user| user.last_name }
What we're interested in here is the return value, which should be an array of last names.
The Getting Started with Rails Guide contains another fun walkthrough, but with additional explanation of Rails architecture.
Discussion