我正在使用Rails构建一个活动应用程序,并试图掌握预订确认流程。目前我的MVC似乎到处都是。
这是我的免费活动预订表格,纯粹需要用户所需的空间数量 -
new.html.erb
<%= simple_form_for [@event, @booking], id: "new_booking" do |form| %>
<% if @booking.errors.any? %>
<h2><%= pluralize(@booking.errors.count, "error") %> prevented this Booking from saving:</h2>
<ul>
<% @booking.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
<% end %>
<div class="form-group">
<p>Please confirm the number of spaces you wish to reserve for this event.</p>
<%= form.input :quantity, class: "form-control" %>
</div>
<p> This is a free event. No payment is required.</p>
<div class="panel-footer">
<%= form.submit :submit, label: 'Confirm Booking', class: "btn btn-primary" %>
<% end %>
</div>
这是我的控制器代码 -
bookings_controller.rb
before_action :find_booking, only: [:show, :update]
before_action :find_event, only: [:show, :update]
before_action :authenticate_user!
def new
@event = Event.find(params[:event_id])
@booking = @event.bookings.new(quantity: params[:quantity])
@booking.user = current_user
end
def create
@event = Event.find(params[:event_id])
@booking = @event.bookings.new(booking_params)
@booking.user = current_user
if @booking.paid_booking
flash[:success] = "Your place on our event has been booked"
@booking.update_attributes!(booking_number: "MAMA" + '- ' + SecureRandom.hex(4).upcase)
redirect_to event_booking_path(@event, @booking)
else
flash[:error] = "Booking unsuccessful"
render "new"
end
end
def free_booking
if @booking.free_booking
@booking.update_attributes!(booking_number: "MAMA" + '- ' + SecureRandom.hex(4).upcase)
redirect_to event_booking_path(@event, @booking)
else
flash[:error] = "Booking unsuccessful"
render "new"
end
end
def show
@event = Event.find(params[:event_id])
@booking = Booking.find_by(booking_number: params[:booking_number])
end
def update
if @booking.save
redirect_to event_booking_path(@event, @booking) , notice: "Booking was successfully updated!"
else
render 'new'
end
end
private
def booking_params
params.require(:booking).permit(:stripe_token, :booking_number, :quantity, :event_id, :stripe_charge_id, :total_amount)
end
def find_booking
@booking = Booking.find_by(booking_number: params[:booking_number])
end
def find_event
@event = Event.find(params[:event_id])
end
当我在控制台上查看预订时,保存的唯一参数是event_id和user_id - 免费预订不会保存数量,但会为付费预订保存。此外,我的预订确认展示视图包含以下属性 -
<%= @event.title %>
<%= @booking.quantity %>
<%= @booking.booking_number %>
在这三个属性中,只有event.title显示在页面上,其他两个属性根本不显示。我完全不知道这是怎么回事。如果有人能看到我无法做到的事情,我将不胜感激。