2015-06-08 2 views
-1

В моей базе данных У меня есть 3 модели: драйверы, оговорки и раздел. Водитель может войти в систему и сделать заказ, где он выбирает дату и раздел. Как я могу сделать веб-страницу с этой информацией? На моей реальной веб-странице драйвер может создать только новую дату, но не может выбрать какой-либо раздел.Рельсы, ссылки разные модели на той же веб-странице

Это _form.html файл резервирования:

<%= form_for(@reservation) do |f| %> 
    <% if @reservation.errors.any? %> 
    <div id="error_explanation"> 
     <h2><%= pluralize(@reservation.errors.count, "error") %> prohibited this reservation from being saved:</h2> 

     <ul> 
     <% @reservation.errors.full_messages.each do |message| %> 
     <li><%= message %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 

    <div class="field"> 
    <%= f.label :date %><br> 
    <%= f.date_select :date, :start_year => Date.current.year, :start_month => Date.current.month, :start_day => Date.current.day %> 
    </div> 
    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 

Я должен добавить что-то здесь?

А вот модели:

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 
    has_one :driver 
    belongs_to :driver 
    belongs_to :section 
    has_one :section 
    belongs_to :leaving 
    has_one :leaving 
    attr_accessible :id, :date 
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 
    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 
end 

reservation_controller.rb

class ReservationsController < ApplicationController 
    before_action :set_reservation, only: [:show, :edit, :update, :destroy] 

    # GET /reservations 
    # GET /reservations.json 
    def index 
    @reservations = Reservation.all 
    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 
    end 

    # POST /reservations 
    # POST /reservations.json 
    def create 
    @reservation = Reservation.new(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).merge(driver_id: current_driver:id) 
    end 
+0

вставить код. – webster

+0

'class Бронирование

+0

Я думал, что модели нужны как – Furla94

ответ

1

Вы также должны поделиться своим кодом контроллера. С предположениями, что у вас есть объект current_user, чтобы определить, какой пользователь (драйвер) является логином и имеет столбец driver_id в таблице оговорок.

Этот код может вам помочь.

Бронирование Контроллер

def create 
@reservation = Reservations.new(reservation_params) 
if @reservation.save 
    redirect_to reservations_path 
else 
    render 'new' 
end 
end 

private 
def reservation_params 
    params.require(:reservation).permit(:date).merge(driver_id: current_user.id) 
end 
+0

@ Furla94.that синтаксис правильный. Вы можете вставить свой контроллер резервирования. Так что я могу найти ошибку. –

+0

Я вставляю его в главное сообщение, потому что он слишком длинный – Furla94

+0

просто вставьте создайте действие и зарезервируйте_парам –