我在rails中做了一个网页,驱动程序可以选择一个公共汽车站,我有模型driver.rb,reservation.rb和sections.rb。 当司机观看预订列表时,我希望他能够看到该部分的名称。我使用委托方法来使用节名称。 问题是我想在名称中分配属性loc_dep和loc_arr的值(两者都是字符串)。 这是代码:
driver.rb
class Driver < ActiveRecord::Base
before_save { self.email = email.downcase }
has_many :reservations
has_secure_password
attr_accessible :name, :surname, :address, :birth_date, :id,
:email, :password, :password_confirmation
validates_uniqueness_of :email
validates :password, :length => { :minimum => 5 } , length: { maximum: 20 }
validates :name, presence: true, length: { maximum: 20 }
validates :surname, presence: true, length: { maximum: 20 }
validates :email, presence: true, length: { maximum: 50 }, uniqueness: { case_sensitive: false }
validates :birth_date, presence: true
end
reservation.rb
class Reservation < ActiveRecord::Base
belongs_to :driver
belongs_to :section
belongs_to :leaving
attr_accessible :id, :date, :section_id, :driver_id, :leaving_id
delegate :name, :to => :driver, prefix: true, :allow_nil => false
delegate :name, :to => :section, prefix: true, :allow_nil => false
end
section.rb
class Section < ActiveRecord::Base
self.inheritance_column = nil
has_many :reservations
has_many :stops
has_one :leaving
attr_accessible :id, :loc_dep, :loc_arr, :type, :time, :leaving_id, :name
validates :type, inclusion: { in: %w(weekday holiday daily scholastic)} , :allow_nil => false
validates :loc_dep, presence: true
validates :loc_arr, presence: true
validates :time, presence: true
def as_field
"#{loc_dep} - #{loc_arr} (#{type} - #{time} min)"
end
end
reservation_controller.rb
class ReservationsController < ApplicationController
before_action :set_reservation, only: [:show, :edit, :update, :destroy]
# GET /reservations
# GET /reservations.json
def index
if current_driver.admin
@reservations = Reservation.all
else
@reservations = current_driver.reservations
end
end
# GET /reservations/1
# GET /reservations/1.json
def show
end
# GET /reservations/new
def new
@reservation = Reservation.new
end
# GET /reservations/1/edit
def edit
@sections=Section.all
end
# POST /reservations
# POST /reservations.json
def create
@reservation = current_driver.reservations.build(reservation_params)
respond_to do |format|
if @reservation.save
format.html { redirect_to @reservation, notice: 'Reservation was successfully created.' }
format.json { render :show, status: :created, location: @reservation }
else
format.html { render :new }
format.json { render json: @reservation.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /reservations/1
# PATCH/PUT /reservations/1.json
def update
respond_to do |format|
if @reservation.update(reservation_params)
format.html { redirect_to @reservation, notice: 'Reservation was successfully updated.' }
format.json { render :show, status: :ok, location: @reservation }
else
format.html { render :edit }
format.json { render json: @reservation.errors, status: :unprocessable_entity }
end
end
end
# DELETE /reservations/1
# DELETE /reservations/1.json
def destroy
@reservation.destroy
respond_to do |format|
format.html { redirect_to reservations_url, notice: 'Reservation was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_reservation
@reservation = Reservation.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
private
def reservation_params
params.require(:reservation).permit(:date, :section_id)
end
end
例如: Section(loc_dep =“New York”,loc_arr =“Boston”,type =“scholastic”,time =“50”)---&gt;所有值都由驱动程序选择
我希望名称的值为“纽约 - 波士顿”。
答案 0 :(得分:0)
不确定我是否理解这里的整个问题,但您可以从Section模式中删除“name”,只需使用方法来推断该值:
#Section.rb
def name
"#{loc_dep} - #{loc_arr}"
end