- What is the difference between the Rails generate scaffold statement
and the Rails generate model statement?
Ans: The Rails generate scaffold statement creates a new database table
together with a controller and the views index, show, new, and edit.
The Rails generate model statement only creates the new database table.
- Write a Ruby line that prints a random number from 0 to 10 with equal
probability.
print rand(11)
- Write a Ruby line that assigns to employee_id, a random number 1001 to
1999 with equal probability.
employee_id = 1001 + rand(999)
print employee_id, "\n"
- Modify Example 17 (Overtime Example) to receive input from the keyboard.
Ans:
Replace the statements
hours_worked = 45.0
hourly_salary = 10.0
by
print "Enter hours worked: "
hours_worked = STDIN.gets.chomp.to_f
print "Enter hourly salary: "
hourly_salary = STDIN.gets.chomp.to_f
- Modify Example 19 (ConsultantBSGenerator Example) to display its
output in a rails view.
Ans: Put all of this code in the code in the controller.
Replace the last statement
puts "Our consultants can help your company " +
verb + " " + adjective + " " + noun + "."
with
@sentence = "Our consultants can help your company " +
verb + " " + adjective + " " + noun + "."
Put this code in the view:
<%= @sentence %>
- Write a Rails application with a view that randomly displays
two of these dice. Also display the sum of the dice.
Use three instance variables @die1, @die2, and @sum, whose values are set in
the controller.
Ans: Create a Rails project named Casino with controller RollDice and view
display. Put this method in the controller
def display
roll1 = 1 + rand(6)
roll2 = 1 + rand(6)
@die1 = roll1.to_s + ".jpg"
@die2 = roll2.to_s + ".jpg"
@sum = roll1 + roll2
end
and this code in the display view
<p><%= image_tag @die1
image_tag @die1 %></p>
<p>The sum of the rolls is <%= @sum %></p>