- Before doing anything else in my app, I must sign in.
- First I will create user sample data.
- Next I create sample data tasks for availabilities and requests.
Users Sample Data
if Rails.env.development?
namespace :dev do
desc "Drops, creates, migrates, and adds sample data to database"
task reset: [
:environment,
"db:drop",
"db:create",
"db:migrate",
"dev:sample_data",
]
desc "Adds sample data for development environment"
task sample_data: [
:environment,
"dev:add_users",
"dev:add_availabilities",
"dev:add_requests",
] do
puts "done adding sample data"
end
task add_users: :environment do
puts "adding users..."
names = ["Sam", "Olivia", "Robbie", "Rashid"]
names.each do |name|
u = User.create(
email: "#{name}@example.com",
username: name,
password: "password",
)
puts "added #{u.email}"
end
end
task add_availabilities: :environment do
puts "adding availabilities..."
end
task add_requests: :environment do
puts "adding requests..."
end
end
end
AvailabilitiesController
In the MVC (Model-View-Controller) pattern, the controller is responsible for handling the data flow between models and views.
Automatically associate an availability with the user who created it in the
class AvailabilitiesController < ApplicationController
#...
# GET /availabilities/new
def new
@availability = current_user.availabilities.build
end
# POST /availabilities or /availabilities.json
def create
@availability = current_user.availabilities.build(availability_params)
end
private
#...
# Only allow a list of trusted parameters through.
def availability_params
params.require(:availability).permit(:start_time, :end_time, :location, :description, :is_open)
# Removed :user_id from params
end
end
Availabilities Sample Data
if Rails.env.development?
namespace :dev do
#...
task add_availabilities: :environment do
puts "adding availabilities..."
locations = ["Movement Lincoln Park", "Movement Wrigley", "BKB", "First Ascent Avondale", "First Ascent Humboldt", "First Ascent Uptown"]
descriptions = ['Seeking a partner for bouldering.', 'Seeking a partner for top rope.', 'Seeking a partner lead climbing.']
users = User.all
users.each do |user|
20.times do
start_time = rand(3.months).seconds.from_now
end_time = start_time + rand(2..6).hours
location = locations.sample
description = descriptions.sample
user.availabilities.create(
start_time: start_time,
end_time: end_time,
location: location,
description: description,
is_open: true
)
end
end
puts "done adding availabilities..."
end
task add_requests: :environment do
puts "adding requests..."
end
end
end
Requests
Set requests to pending
class RequestsController < ApplicationController
#...
def create
@request = Request.new(request_params)
@request.status = :pending
#...
automatically set the sender of a request to the creator of the request
class RequestsController < ApplicationController
# ...
def create
@request = current_user.sent_requests.new(request_params)
# Set the sender_id for the new request to be the ID of current_user
#...
def request_params
params.require(:request).permit(:availability_id, :status)
# Remove :sender_id from params
end
end
Request an availability
<!--/workspaces/Belay-Board/app/views/availabilities/index.html.erb-->
<p style="color: green"><%= notice %></p>
<h1>Availabilities</h1>
<div id="availabilities">
<% @availabilities.each do |availability| %>
<%= render availability %>
<p>
<%= link_to "Request this availability", new_request_path(availability_id: availability.id) %>
<!--This link will direct to the new request form with the selected availability's ID-->
</p>
<% end %>
</div>
class RequestsController < ApplicationController
# ...
def new
@availability = Availability.find(params[:availability_id]) if params[:availability_id]
@request = Request.new(availability: @availability)
# Modify the `new` action to accept an `availability_id` parameter and initialize a new `Request` with this availability.
end
# ...
end
Request Sample Data
namespace :dev do
# ...
task add_requests: :environment do
puts "adding requests..."
availabilities = Availability.all
availabilities.each do |availability|
# Randomly select a user who will make the request
requester = User.all.sample
# Create a request for the selected availability
availability.requests.create(
sender: requester,
status: :pending
# Add any other attributes you want for the request here
)
puts "Request created by #{requester.username} for availability ID #{availability.id}"
end
puts "done adding requests..."
end
end
Next Steps
Now I have sample data for Users, Availabilities, and Requests. Next, I will create a page to display the requests made on availabilities cerated by the current user so the user can accept or reject the request.
Top comments (0)