我目前正在开展rails 5项目,其中我想在运输中添加一名乘客。在乘客控制器索引动作中,我设置运输由params id搜索。但即使它已被创建,它也会返回nil。
以下是路线:
user_protest_transportation_passengers GET /users/:user_id/protests/:protest_id/transportations/:transportation_id/passengers(.:format) passengers#index
POST /users/:user_id/protests/:protest_id/transportations/:transportation_id/passengers(.:format) passengers#create
new_user_protest_transportation_passenger GET /users/:user_id/protests/:protest_id/transportations/:transportation_id/passengers/new(.:format) passengers#new
edit_user_protest_transportation_passenger GET /users/:user_id/protests/:protest_id/transportations/:transportation_id/passengers/:id/edit(.:format) passengers#edit
user_protest_transportation_passenger GET /users/:user_id/protests/:protest_id/transportations/:transportation_id/passengers/:id(.:format) passengers#show
PATCH /users/:user_id/protests/:protest_id/transportations/:transportation_id/passengers/:id(.:format) passengers#update
PUT /users/:user_id/protests/:protest_id/transportations/:transportation_id/passengers/:id(.:format) passengers#update
DELETE /users/:user_id/protests/:protest_id/transportations/:transportation_id/passengers/:id(.:format) passengers#destroy
user_protest_transportations GET /users/:user_id/protests/:protest_id/transportations(.:format) transportations#index
POST /users/:user_id/protests/:protest_id/transportations(.:format)
这是链接:
<div class="card">
<div class="card-block">
<h3 class="card-title">Need A Ride?</h3>
<p class="card-text">Find people near you willing to share a seat who are coming to the event. We are all in this together!.</p>
<%= link_to 'Get A Ride', new_user_protest_transportation_passenger_path(@user, @protest, @transportation), class: "btn btn-primary" %>
</div>
</div>
以下是乘客控制员:
class PassengersController < ApplicationController
def index
@transportation = Transportation.find(params[:transportation_id])
end
def new
end
def create
@passenger = Passenger.new(passenger_params)
@user = User.find(current_user.id)
@transportation = Transportation.find(params[:transportation_id])
@driver = User.find(@transportation.transporter_id)
@protest = Protest.find(@transportation.destination_id)
@transportations = Transportation.where(destination_id: @protest.id)
has_ride = false
@transportations.each do |transportation|
passenger = Passenger.find_by user_id: current_user.id
unless (passenger.nil?)
has_ride = true
end
end
if has_ride
redirect_to protests_path, notice: "You already have a ride."
return
end
respond_to do |format|
if @passenger.save
format.html { redirect_to user_protest_transportation_passenger_path(id: @passenger.id), notice: 'Passenger was successfully created.' }
else
format.html { render :new }
end
end
end
def show
@driver = get_driver
if @driver
@transportation = Transportation.where(transporter_id: @driver.id).first!
@protest = Protest.find(@transportation.destination_id)
end
end
def edit
end
def update
end
def destroy
@passenger = Passenger.find_by(user_id: params[:user_id], transportation_id: params[:transportation_id])
if @passenger
@passenger.destroy
redirect_to user_protests_path, notice: 'Ride was canceled.'
else
redirect_to user_protest_transportations_path, notice: 'Error: the passenger record failed to delete.'
end
end
private
def get_driver
driver = nil
p = Passenger.find_by(user_id: current_user.id, transportation_id: params[:transportation_id])
t = Transportation.find(params[:transportation_id])
driver = User.find(t.transporter_id)
end
def passenger_params
params.permit(:user_id, :transportation_id)
end
end
我试图在params中获取运输ID,我尝试使用session[transportation_id]
查找,但似乎无法正常工作。
抗议控制员:
class ProtestsController < ApplicationController
def index
@protests = Protest.all
end
def show
@protest = Protest.find(params[:id])
@user = current_user
end
def new
@protest = Protest.new
end
def edit
@protest = Protest.find(params[:id])
end
def create
@protest = Protest.new(protest_params)
@protest.creator = current_user
respond_to do |format|
if @protest.save
format.html { redirect_to @protest, notice: 'Protest was successfully created.' }
else
format.html { render :new }
end
end
end
def update
@protest = Protest.find(params[:id])
respond_to do |format|
if @protest.update(protest_params)
format.html { redirect_to @protest, notice: 'Protest was successfully updated.' }
else
format.html {render :edit }
end
end
end
def destroy
@protest = Protest.find(params[:id])
@protest.destroy
redirect_to protests_path
end
private
def protest_params
params.require(:protest).permit(:name, :description, :location, :starts_at, :creator, :image)
end
end