如何将用户与他自己的预订链接链接

时间:2015-07-29 14:39:38

标签: ruby-on-rails ruby

你可以帮我解决这个问题吗? 我有预订和用户,我想从用户档案中看到他所做的预订。这是我到目前为止写的代码:

 app/views/reservations/index.html.erb
.
.
.


    <tbody>
        <% @reservations.each do |reservation| %>
          <tr>
            <td><%= reservation.Class_point %></td>
            <td><%= reservation.Date_trip %></td>
            <td><%= reservation.dep.try(:city) %></td>
            <td><%= reservation.arr.try(:city) %></td>
            <td><%= reservation.route.countStop %></td>
            <td><%= reservation.user_id %></td>
            <td><%= link_to 'Show', reservation %></td>
            <td><%= link_to 'Edit', edit_reservation_path(reservation) %></td>
            <td><%= link_to 'Destroy', reservation, method: :delete, data: { confirm: 'Are you sure?' } %></td>
.
.
.

app/controllers/reservations_controller

class ReservationsController < ApplicationController
  before_action :logged_in_user, only: [:edit, :new, :update, :destroy]
  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.
    def reservation_params
      params.require(:reservation).permit(:Class_point, :Date_trip, :dep_id, :arr_id, :route_id, :user_id)
    end

  def logged_in_user
      unless logged_in?
        flash[:danger] = "Please log in."
        redirect_to login_url
      end
  end
end



app/models/reservation.rb

class Reservation < ActiveRecord::Base
  belongs_to :dep ,:class_name => 'Stop', :foreign_key => 'dep_id'
  belongs_to :arr ,:class_name => 'Stop',:foreign_key => 'arr_id'
  belongs_to :route
  belongs_to :user
  delegate :CountStop, :to => :route, prefix: true, :allow_nil => false
  delegate :city ,:to => :arr, :allow_nil => false
  delegate :city ,:to => :dep, :allow_nil => false
end

问题在于,当我创建新的预订时,user_id不会出现.. 我能做什么?谢谢

2 个答案:

答案 0 :(得分:1)

您没有使用reservation的值创建user_id,因此user_id始终为 nil 。您可以更改下面的reservation_params以使其正常工作。

def reservation_params
  params.require(:reservation).permit(:Class_point, :Date_trip, :dep_id, :arr_id, :route_id).merge(user_id: current_user.id)
end

并且 @engineersmnky 指出,您应该使用:class_point:date_trip代替:Class_point,{{1 }}

答案 1 :(得分:0)

我很想看到模型......如果预订属于用户,在预约的编辑控制器中你可以简单地去@reservation = Reservation.find(params [:id])和然后@ reservation.user将为您提供用户的信息