我正在构建一个应用程序,允许:用户放置:订单:地点。我在PlacesController中定义 orders_params ,但Rails说ParameterMissing(“ params缺失或值为空:order ”)
如何修复此问题,以便可以通过 app / place / PLACE_ID / book 创建:place_id的订单?
PlacesController:
def book
@place = Place.find(params[:id])
@order = Order.new(order_params)
if @order.save
flash[:success] = "Success"
redirect_to @place
else
render 'book'
end
end
private
def place_params
params.require(:place).permit(:place_name)
end
def order_params
params.require(:order).permit(:place_id, :user_id)
end
的routes.rb
get 'book' => 'places#show#book'
get '/places/:id/book' => 'places#book', as: :place_book
resources :places
OrdersController
class OrdersController < ApplicationController
def new
end
def create
@place = Place.find(@order.place_id)
@order = Order.new(user_id: current_user.id, place_id: @place)
if @order.save
flash[:success] = "Order placed"
redirect_to @place
else
render 'book'
end
end
def show
end
def edit
end
def update
end
def destroy
end
private
def order_params
params.require(:order).permit(:place_id, :user_id)
end
端
型号:Order.rb
belongs_to :user
has_one :place
validates :user_id, presence: true
validates :place_id, presence: true
## OTHER VALIDATIONS
查看:app / views / places / book
<%= form_for(@order) do |f| %>
<!-- SOME FORM_FORS -->
<%= f.submit "Place order", class: "btn btn-primary" %>
<% end %>