====== אפליקצה ראשונה עם רובי און ריילס ====== זהו מדריך כללי על ההתקנה והעבודה עם רובי און ריילס ===== חלק 1 - התקנה ===== הדבר הראשון שיש לעשות, הוא להתקין את רובי ורובי און ריילס על מערכת ההפעלה שלך ולהתקין מסד נתונים. מסד הנתונים הרשמי של ריילס הוא SQLite, אך ניתן להשתמש במגוון רחב של מסדי נתונים אחרים. מדריכי התקנה מפורטים לרובי און ריילס[[getting-started/installation/mac|למקינטוש]], [[getting-started/installation/windows|לחלונות]], ו[[getting-started/installation/linux|לינוקס]]. ===== חלק 2 - יוצרים את האפליקציה ===== - צור פרוייקט ריילס ע"י מתן הפקודה $ rails myapp בשורת הפקודה שלך. עבור לתיקיה החדשה שנוצרה ע"י הקשה של הפקודה $ cd myapp הפעל את שרת האפליקציה שלך $ ruby script/server //{{:getting-started:installation:information.png|}}Note: פקודה זו תריץ שרת אפליקציה המתאים לסביבות פיתוח בלבד// - פתח את הדפדפן וגש אל הכתובת http://localhost:3000 ואם הכל הלך כשורה, תוכל לראות את דף ברירת המחדל של האפליקציה שלך. ===== פרק 3 - יצירת מסד הנתונים ומבנה בסיסי ===== פתח את האפליקציה שלך בעורך לבחירתך וגש לקובץ config/database.yml זהו הקובץ בו נשמרות ההגדרות של הגישה למסד הנתונים של האפליקציה, הגדרות כמו שם משתמש וסיסמא, שרת וסוג מסד הנתונים. בדרך כלל תדרש לעשות שינויים כדי להפנות את האפליקציה למסד הרלוונטי, במקרה של האפליקציה הזו, אין צורך בשינויים. שים לב ששם מסד הנתונים הדיפולטי נגזר משם האפליקציה שבחרת קודם פתח חלון שורת פקודה, גש לספרית הפרוייקט והקש את הפקודה $ rake db:create ריילס תיצור את מסד הנתונים שלך לסביבת הפיתוח, כפי שציינת בקובץ ההגדרות של מסד הנתונים עכשיו ניצור את הטבלה הראשונה שלנו ואת הממשק הבסיסי לניהול הנתונים שלה, ממשק זה נקרא [[http://guides.rubyonrails.org/getting_started_with_rails.html#_getting_up_and_running_quickly_with_scaffolding|פיגום]] ומשמש למטרה הזו בלבד. //{{:getting-started:installation:information.png|}} הערה: למרות שזה מאוד מפתה, פיגומים נועדו למטרות תחזוקה ובניה ראשונית ולא מומלץ להשתמש בהם מעבר לשימושים הנוגעים למהלך הפיתוח של האפליקציה.// כדי לראות איך משתמשים בפיגומים, נראה למחולל הפיגומים ע"י הפקודה $ ruby script/generate scaffold נניח שברצוננו ליצור טבלת משתמשים, כדי ליצור טבלת משתמשים בעזרת פיגום נקיש את הפקודה הבאה $ ruby script/generate scaffold user first_name:string last_name:string active:boolean פקודה זו מייצרת לנו קונטרולר לניהול משתמשים, מודל לייצוג משתמש ממסד הנתונים, את מבנה הטבלה במסד הנתונים ואת דפי ה-html הנחוצים לנו לשם ביצוע פעולות על רשומות משתמשים. //{{:getting-started:installation:information.png|}} הערה: מחולל הפיגום לא יצר את הטבלה במסד הנתונים, אנו צריכים להקיש עוד פקודה בטרם הטבלה תיווצר במסד הנתונים// כדי ליצור את הטבלה נצטרך להריץ פקודת [[http://guides.rubyonrails.org/getting_started_with_rails.html#_running_a_migration|הגירה]] למסד הנתונים $ rake db:migrate עכשיו נוכל לגשת לדפדן ולהקיש את הכתובת הבאה כדי להתחיל לנהל את המשתמשים במערכת http://localhost:3000/users בממשק זה תוכל להוסיף משתמשים, לצפות באחד או בכולם, לערוך ואף למחוק משתמשים. ===== Part 4 - Adding Functionality ===== - We just realized that we forgot to add a bio field for a user - oops! We could either create a new migration to add that column, or roll back the migration that was created for us. Let's do the latter. To remove the table so we can add a column, run this command:$ rake db:rollback This will roll back one migration. - Now open the migration located in the ''/db/migrations/'' directory. It should be the only file. - Notice how the migration has an ''up'' method to migrate up, and ''down'' method to migrate down. - We want to add that bio field, so after the ''t.boolean :active'' line, add: t.text :bio - Save the file, and run ''rake db:migrate'' again to migrate your database up: $ rake db:migrate - If you checked your browser at this point, you'd notice that the bio field isn't automatically showing up in our user's information or forms. We will need to add this ourselves. Yes, we could have erased the files and run the scaffold command to get this field, but then you wouldn't learn anything. - Lets open up the ''/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.

<%= f.label :bio %>
<%= f.text_area :bio %>

- Now open up the New User page in your browser http://localhost:3000/users/new. You should see the bio field, and be able to add new users with a bio. {{:getting-started:installation:information.png|}} 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. - When you create a user, you're brought automatically to his/her show page. Let's add the bio to the show page. You can do this by opening the ''/app/views/users/show.html.erb'' file, and underneath the Active field, adding:

Bio: <%=h @user.bio %>

- Save the file and check it out in the browser. If you don't see your change at first, make sure you are on the ''show'' page (and not the ''index''). When you get to this part, let out a barbaric "woot!", you've made some great progress. ===== Part 5 - Your First Validation ===== - In Rails, we put our validations in our model files, so let's open ''/app/models/user.rb''. - We want to require that a user enter their first name and last name, so let's add validation to our User model, so it ends up looking like this: class User < ActiveRecord::Base validates_presence_of :first_name, :last_name end - Save the file, go to your browser, and attempt to create a user without a first name or last name. - Rails has many helper validation classes, and you can see a list on the right side [[http://apidock.com/rails/ActiveRecord/Validations/ClassMethods|of this API page]]. {{:getting-started:installation:information.png|}} For extra credit, add another validation to your app just for fun. ===== Part 6 - Modifying the Layout ===== - If you viewed the browser HTML source of your application, you may have noticed that your application is in some sort of layout. By default, when you ran the ''scaffold'' command, a ''/app/views/layouts/users.html.erb'' file was created, and used as your layout. - You'll probably only want to use one layout for most of your controllers, so let's give this a more generic name. Rename ''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). - Let's add something to our layout just for fun. Open up the ''application.html.erb'' and add something like:

Rails Rocks!

- Save the file, go to your browser, and you should see the change you made reflected on ALL pages of the website. ===== Part 7 - Having fun with the console ===== - Let's have some fun playing with our User Model. In your command prompt, type:$ ruby script/console You are now in a Ruby IRB session which has access to your models. - Before we start playing, run the following command so that we can see what SQL commands are going to be generated for us. $ ActiveRecord::Base.logger = Logger.new(STDOUT) - Let's try a simple Ruby statement to make sure everything is working:puts "Hello Dude!" - Now let's try having some fun with our model. Create a new model by doing something like this: u = User.new u.first_name = "Gregg" u.last_name = "Pollack" u.save Notice the SQL that is generated. - Now try doing it all in a single line: User.create(:first_name => "Joe", :last_name => "Blow") If you hit any errors, check for syntax problems. - Let's fetch the user with first name of Joe, and change his last name to "Johnson". u = User.find_by_first_name("Joe") u.last_name = "Johnson" u.save Notice the SQL that gets generated; nice! - Now let's delete the user. u.destroy - Have some fun trying the following commands to see what they do: 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. - Let's print out all the users in your system in upper case: User.all.each {|user| puts user.first_name.upcase } - You'll notice that it prints out the first names, and then it also returns the array of Users. Remember, every method in Ruby returns something, even if it's nil. - Let's create an array of all of the last names in our system: 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. - Take a look at the [[http://apidock.com/rails/ActiveRecord/Base|API page here]]. Along the right you'll see all of the methods you can run on ActiveRecord objects. Try a few. ===== Additional Reading ===== The [[http://guides.rubyonrails.org/getting_started.html|Getting Started with Rails Guide]] contains another fun walkthrough, but with additional explanation of Rails architecture.