在不同的控制器中呈现表单时出错

时间:2016-12-04 13:04:04

标签: ruby-on-rails ruby ruby-on-rails-5

我正在尝试在app/views/requestforpms/_form.html.erb中呈现表单app/views/passengerrides/show.html.erb,但在First argument in form cannot contain nil or be empty处收到错误<%= form_for @requestforpm do |form| %>

应用程序/视图/ passengerrides / show.html.erb:

<%= render partial: 'requestforpms/form' %>

应用程序/视图/ requestforpms / _form.html.erb:

<%= form_for @requestforpm do |form| %>
   <%= form.hidden_field :passengerride_id, :value => @passengerride.id %>
   <%= form.hidden_field :customer_id, :value => @current_user.id %>
   <%= form.hidden_field :status, :value => 'Request' %>
   <%= form.submit %>
<% end %>

应用/控制器/ requestforpms_controller.rb:

class RequestforpmsController < ApplicationController

before_action :set_requestforpms, only: [:show, :edit, :update, :destroy]

 def create
  @passengerride =  Passengerride.find (params[:passengerride_id])
  @requestforpm = @passengerride.requestforpms.new(requestforpm_params)

  respond_to do |format|
   if @requestforpm.save
    format.html { redirect_to @passengerride, notice: 'Comment was successfully created.' }
    format.json { render json: @requestforpm, status: :created, location: @requestforpm }
   else
    format.html { render :new }
    format.json { render json: @requestforpm.errors, status: :unprocessable_entity }
   end
  end
 end

 private
  def set_requestforpms
   @requestforpm = Requestforpm.find(params[:id])
  end


  def requestforpms_params
   params.require(:requestforpm).permit(:passengerride_id, :customer_id, :status)
  end
end

修改

应用/控制器/ passengerrides_controller.rb:

class PassengerridesController < ApplicationController

 before_action :para_passengerride, only: [:edit, :update, :show]
 load_and_authorize_resource :except => :create

 def index
  @q = Passengerride.ransack(params[:q])
  @passengerrides = @q.result(distict: true)
 end

 def new
  @passengerride = Passengerride.new
 end

 def show
 end

 def edit
 end

def create

 @passengerride = Passengerride.new(passengerride_params)
 @passengerride.user = current_user
 if @passengerride.save
  flash[:success] = 'Ride was successfully created.'
  redirect_to passengerride_path(@passengerride)
 else
  render 'new'
 end
end

def update

 @passengerride.user = current_user
 if @passengerride.update(passengerride_params)
  flash[:success] = 'Ride was successfully updated'
  redirect_to passengerride_path(@passengerride)
 else
  render 'edit'
 end
end


def destroy
 @passengerride = Passengerride.find(params[:id])
 @passengerride.destroy
 flash[:danger] = "Ride has been deleted successfully"
 redirect_to passengerrides_path
end

private

 def para_passengerride
  @passengerride = Passengerride.find(params[:id])
 end

private

 def passengerride_params
  params[:passengerride][:sharegasmoney] = params[:passengerride][:sharegasmoney].join(', ')
  params[:passengerride][:okwithgender] = params[:passengerride][:okwithgender].join(', ')
  params[:passengerride][:okwith] = params[:passengerride][:okwith].join(', ')
  params.require(:passengerride).permit(:origin, :destination, :departuretime, :departuredate, :okwithgender, :okwith, :sharegasmoney, :description)
 end

0 个答案:

没有答案