用户可以进行预订,每次预订都可以多次出现。在他们输入数据后,他们会获得一个预览页面,以便在信息保存到数据库之前对其进行确认。出现信息将传递到预览页面。我可以在页面底部的参数转储中看到它。我似乎无法访问它以显示给用户。
这是我的代码:
reservations_controller.rb
def new
@reservation = Reservation.new
@occurrence = @reservation.occurrence.build
end
def preview
if params[:reservation]
@reservation = Reservation.new(reservation_params)
@occurrence = @reservation.occurrence.build(params[:occurrence])
end
end
private
def reservation_params
params.require(:reservation).permit(:title, :description, :is_public, :is_internal, :internal_organization, :contact_name,
:contact_phone, :contact_email, :contact_present, :contact_present_name,
:contact_present_phone, :contact_present_email, :notes)
end
reservations / new.html.erb(我在嵌套表单周围添加了一些空行以便于查找):
<div id="addReservationForm_container" class="addReservationFormContainer formContainer">
<%= form_for @reservation, :url => preview_path, :html => { :method => :get } do |f| %>
<% if @reservation.errors.any? %>
<div class="error">
Please address the <%= "#{'error'.pluralize(@reservation.errors.count)}" %> below to submit this reservation.
</div>
<% end %>
<%= field_set_tag 'Reservation Information' do %>
<div class="formField paragraph <%= 'error' if @reservation.errors.include?(:title) %>">
<%= f.label :title, 'Title:' %>
<%= f.text_field :title, size: "40", class: (@reservation.errors.include?(:title) ? 'error' : '') %>
</div>
<div class="formField">
<%= f.label :description, 'Enter a short description of the event:' %>
<%= f.text_area :description, size: "60x6" %>
</div>
<div class="formField">
<%= f.label :is_public do %>
Should this event be listed on public calendars?
<%= f.select :is_public, options_for_select([['Yes', true], ['No', false]]) %>
<% end %>
</div>
<hr />
<div class="formField">
<%= f.label :is_internal do %>
Is this reservation for a related event?
<%= f.select :is_internal, options_for_select([['Yes', true], ['No', false]]) %>
<% end %>
</div>
<div class="internalDetails formField <%= 'error' if @reservation.errors.include?(:internal_organization) %>">
<%= f.label :internal_organization, 'Please enter the name of the department, organization, or club hosting the event:' %>
<%= f.text_field :internal_organization, size: '40', class: (@reservation.errors.include?(:internal_organization) ? 'error' : '') %>
</div>
<% end %>
<%= field_set_tag 'Date and Time' do %>
<div class="paragraph">
Select the date(s) and time(s) you want to reserve:
<div class="formField paragraph">
<%= f.fields_for :occurrences do |o| %>
<%= o.text_field :start_date, class: 'datepicker' %>
<%= o.select :end_date, options_for_select(create_hours(:start_time => 7.5.hours, :end_time => 21.hours, :increment => 15.minutes)) %>
<% end %>
</div>
</div>
<% end %>
<%= field_set_tag 'Contact Information' do %>
<div class="paragraph">
Please enter the contact information of the person responsible for the reservation:
</div>
<div class="formField paragraph <%= 'error' if @reservation.errors.include?(:contact_name) %>">
<%= f.label :contact_name, 'Name:' %>
<%= f.text_field :contact_name, size: "40", class: (@reservation.errors.include?(:contact_name) ? 'error' : '') %>
</div>
<div class="formField paragraph <%= 'error' if @reservation.errors.include?(:contact_phone) %>">
<%= f.label :contact_phone, 'Phone:' %>
<%= f.text_field :contact_phone, size: "40", class: (@reservation.errors.include?(:contact_phone) ? 'error' : '') %>
</div>
<div class="formField paragraph <%= 'error' if @reservation.errors.include?(:contact_email) %>">
<%= f.label :contact_email, 'E-mail:' %>
<%= f.text_field :contact_email, size: "40", class: (@reservation.errors.include?(:contact_email) ? 'error' : '') %>
</div>
</div>
<% end %>
<%= field_set_tag 'Additional Information' do %>
<div class="internalDetails formField">
<%= f.label :notes, 'Please enter any additional information or instructions:' %>
<%= f.text_area :notes, size: "60x6" %>
</div>
<% end %>
<%= f.submit 'Continue' %>
<% end %>
</div>
以下代码似乎无法显示嵌套表单中的信息。
保留/ preview.html.erb
<div class="paragraph">
Please look over the information you entered for your reservation, and then click either
the "Submit Reservation" button to submit your request, or the "Make Changes" button to
return to the previous form and edit the request.
</div>
<div style="margin-left: 3em;">
<div class="paragraph">
<div> <%= @reservation.title %></div>
<div> <%= @occurrence.start_date %></div>
<div> <%= @reservation.contact_name %> (<%= @reservation.contact_email %>)</div>
<div> <%=@reservation.contact_phone %></div>
</div>
</div>
reservation.rb
has_many :occurrence
accepts_nested_attributes_for :occurrence, :reject_if => :all_blank
我是Rails的新手,所以发现我在这里遇到了多个错误并不会让我感到惊讶。