从其他控制器调用创建动作

时间:2019-03-08 18:46:15

标签: ruby-on-rails ruby-on-rails-5.2

我想从bookings#newrooms#show创建预订。当我尝试从bookings#new创建它时,它可以工作,但是当尝试从rooms#show创建它时,它向我显示错误:

  

1个错误,禁止保存该预订,房间必须存在。

这是我正在使用的代码:

BookingsController:

def create
  if @room          
    @room = Room.find(params[:room_id])
    @booking = @room.bookings.create(booking_params)
    if @booking.save
      redirect_to room_path(@room)
    else
      render :new
    end        
  else 
    @booking = Booking.new(booking_params)
    respond_to do |format|
      if @booking.save
        format.html { redirect_to @booking, notice: 'Booking was successfully created.' }
        format.json { render :show, status: :created, location: @booking }
      else
        format.html { render :new }
        format.json { render json: @booking.errors, status: :unprocessable_entity }
      end
    end
  end
end

views / rooms / show.html.erb

<h2>book this room:</h2>
<%= form_with(model: [ @room, @room.bookings.build ], local: true) do |form| %>
  <p>
    <%= form.label :traveller %>
    <%= form.text_field :traveller %>
  </p>

 <p>
    <%= form.label :startfrom %>
    <%= form.datetime_select :startfrom %>
  </p>

  <p>
    <%= form.label :endsat %>
    <%= form.datetime_select :endsat %>
  </p>

  <p>
    <%= form.label :bookingref %>
    <%= form.text_field :bookingref %>
  </p>

  <p>
    <%= form.submit %>
  </p>
<% end %>

1 个答案:

答案 0 :(得分:1)

您的错误在控制器的if-else内部。您在定义@room之前先进行检查,因此它始终为nil。应该是:

def create
  # use find_by here, otherwise you get RecordNotFound error
  @room = Room.find_by(id: params[:room_id])
    if @room
      # use build, because create saves the instance
      @booking = @room.bookings.build(booking_params)
      if @booking.save
        redirect_to room_path(@room)
      else
        # I suppose you don't want render bookings/new view here
        render 'books/show'
      end
    else 
      @booking = Booking.new(booking_params)
      respond_to do |format|
        # redirect and render logic goes here. BTW, do you really need json response format?
      end
    end
  end
end

还要在房间里定义#show动作

@booking = @room.bookings.build

并使用表单中的实例正确显示验证错误

form_with(model: [@room, @booking], local: true) do |form|