我试图在铁轨上制作租赁应用程序。
我一直坚持要显示start_date
和end_date
,然后解析bookings_controller
。
我列出了items_controller
中的项目,然后有add_to_cart_controller
来向用户展示'要求。
我想将其解析为bookings_controller
,以记录用户提出的请求,然后记录项目的可用性。
%input{:type =>'text',
:placeholder => params[:startdate] != nil ? params[:startdate] : "from date",
:id => 'startdate',
:name => "startdate",
:style => 'width: 100px'}
%input{:type =>'text',
:placeholder => params[:startdate] != nil ? params[:enddate] : "to date",
:id => 'enddate',
:name => "enddate",
:style => 'width: 100px'}
我已经使用设备部分工作进行身份验证,然后用户可以选择自行车并将其添加到购物车。
我想得到:
用户的开始和结束日期。
添加日历功能以及用户需要选择的开始日期和结束日期。
这应该转到bookings_controller
,它会记录所有租借并更新自行车'可用性。
推车控制器
class CartController < ApplicationController
before_action :authenticate_user!
def add
# get the ID of the product
id = params[:id]
# if the cart is already been created, use the existing cart
# else create a blank cart
if session[:cart] then
cart = session[:cart]
else
session[:cart] = {}
cart = session[:cart]
end
# if the product has already been added to the cart, increment the
# else set the value to 1
if cart[id] then
cart[id] = cart[id] + 1
else
cart[id] = 1
end
# redirect to the cart display page
redirect_to :action => :index
end
def clearCart
# set the session variable to nil and redirect
session[:cart] = nil
redirect_to :action => :index
end
def index
# if there is a cart, pass it to the page for display
# else pass an empty value
if session[:cart] then
@cart = session[:cart]
else
@cart = {}
end
end
end
项目控制器
class ItemsController < ApplicationController
before_action :authenticate_user!
before_action :ensure_admin, :only => [:edit, :destroy]
before_action :set_item, only: [:show, :edit, :update, :destroy]
# GET /items Only admin can edit delete items
# GET /items.json
def index
@items = Item.all
end
# GET /items/1
# GET /items/1.json
def show
end
# GET /items/new
def new
@item = Item.new
end
# GET /items/1/edit
def edit
end
# POST /items
# POST /items.json
def create
puts "debug +++++++++++++++++++++++++++++++++++++++++++++++++++"
puts params.inspect
puts "debug +++++++++++++++++++++++++++++++++++++++++++++++++++"
@item = Item.new(item_params)
respond_to do |format|
if @item.save
format.html { redirect_to @item, notice: 'Item was successfully created.' }
format.json { render :show, status: :created, location: @item }
else
format.html { render :new }
format.json { render json: @item.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /items/1
# PATCH/PUT /items/1.json
def update
respond_to do |format|
if @item.update(item_params)
format.html { redirect_to @item, notice: 'Item was successfully updated.' }
format.json { render :show, status: :ok, location: @item }
else
format.html { render :edit }
format.json { render json: @item.errors, status: :unprocessable_entity }
end
end
end
# DELETE /items/1
# DELETE /items/1.json
def destroy
@item.destroy
respond_to do |format|
format.html { redirect_to items_url, notice: 'Item was successfully destroyed.' }
format.json { head :no_content }
end
end
def ensure_admin
unless current_user && current_user.admin?
render :text => "Access Error Message", :status => :unauthorized
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_item
@item = Item.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def item_params
params.require(:item).permit(:title, :description, :price, :image_url, :category, :brand)
end
end
购物车视图
<h1>Your Cart</h1>
<% if @cart.empty? %>
<p>Your Cart is empty</p>
<% end %>
<% total = 0 %>
<table class="cart">
<tr>
<td class="legend"></td>
<td class="legend">Item</td>
<td class="legend">Price</td>
<td class="legend">Length</td>
<td class="legend">Total</td>
</tr>
<% @cart.each do | id, quantity | %>
<% item = Item.find_by_id(id) %>
<tr>
<td>
<div class="image">
<%= link_to (image_tag item.image_url, :style => "height:40px"), item %>
</div>
</td>
<td class="title"><%= link_to item.title, item %></td>
<td class="price"><%= number_to_currency(item.price) %></td>
<td class="quantity"><%= quantity %><br /></td>
<td class="price"><%= number_to_currency(quantity * item.price, :unit => "€") %></td>
%input{:type =>'text', :placeholder => params[:startdate] != nil ? params[:startdate] : "from date", :id => 'startdate', :name => "startdate", :style => 'width: 100px'}
%input{:type =>'text', :placeholder => params[:startdate] != nil ? params[:enddate] : "to date", :id => 'enddate', :name => "enddate", :style => 'width: 100px'}
</tr>
<% total += quantity * item.price %>
<% end %>
<tr>
<td colspan="4">
<div class="total">Total:</div>
</td>
<td>
<div class="price"><%= number_to_currency(total, :unit => "Eur") %></div>
</td>
</tr>
</table>
<p>
<%= link_to 'Make a booking', :controller => :items %>
</p>
的routes.rb
Rails.application.routes.draw do
resources :bookings
get 'bookings/index'
get 'bookings/show'
get 'bookings/new'
get 'bookings/edit'
get 'bookings/destroy'
resources :profiles
get 'bookings/index'
get 'bookings/show'
get 'bookings/new'
get 'bookings/edit'
get 'bookings/destroy'
resources :profiles
devise_for :users
get '/about' => 'site#about'
get '/contact' => 'site#contact'
get '/admin' => 'user#admin_login'
get '/logout' => 'user#logout'
get '/cart' => 'cart#index'
get '/cart/:id' => 'cart#add'
get '/cart/clear' => 'cart#clearCart'
get '/signedinuserprofile' => 'profiles#signedinuserprofile'
get '/product' => 'catalog#view'
resources :items
# You can have the root of your site routed with "root"
root 'items#index'
# Example of regular route:
get 'products/:id' => 'catalog#view'
# Example of named route that can be invoked with purchase_url(id: product.id)
get 'products/:id/purchase' => 'catalog#purchase', as: :purchase
end
模型关系
用户
has_one :profile
has_many :items
has_many :bookings
资料
belongs_to :user
项目
belongs_to :user
has_many :bookings
预订
belongs_to :item
我的主要问题是让应用程序接受开始和结束日期以进行租赁预订。有谁知道如何解决这个问题?
我也想知道如何在预订时显示自行车的可用性。